2

Looking for help solving transcendental equations in Matlab. Here's an equation for example:

1/2 = cos(x)cos(2x) ; 0<=x<=pi/2 solving for x:

x = acos((1/2)(sec(2x))

I have tried using intersect() along with various other Matlab functions. It's easy to see an approximate value of ~.48 when I plot using the following code:

x = 0:(pi/2)/1000:pi/2;
f = @(x) (1/2)*acos((1/2)*sec(x));
plot(x,f(x));

How can I have Matlab return the value where x == f(x) within a certain tolerance?

btobers
  • 175
  • 1
  • 1
  • 7
  • Your problem is equivalent to `cos(x)=z` for `z` any of the solutions of `0.5=2*z^3-z` so that you have a polynomial roots problem and a trigonometric inversion,both relatively easy. – Lutz Lehmann Feb 07 '19 at 15:35

1 Answers1

1

For finding a numerical solution it doesn't really matter if you have a polynomial or even a transcendental equation. In general for your particular problem there are two nice built-ins: fzero tries to find a root of a function f, that is a value x where f(x) == 0. You need to provide an initial estimate but you cannot provide bounds. Then there is fminbnd which minimizes your function, so you have to write your problem as a minimization problem. But in this case you can provide bounds:

format long
% find a root (unbounded)
f=@(x)1/2 - cos(x).*cos(2*x);
z = fzero(f,0,optimset('TolX',1e-5));
disp(z);

% find a minimum (bounded)
g=@(x)(f(x)).^2;
z = fminbnd(g,0,pi/2,optimset('TolX',1e-5));
disp(z);

Try it online!

flawr
  • 10,814
  • 3
  • 41
  • 71