Using Matlab to solve $\sin(k+a)=x\cos(k)$ numerically for $a$

151 Views Asked by At

I want to solve a trigonometric equation numerically for $a$ via Matlab

$$\sin(k+a)=x\cos(k)$$

where $a$ is an argument of a complex number. Both $a$ and $k$ vary between $0\leq k\leq\pi$.

I want to have solution sol[x,k], so that for each $x$ and $k$ I get (either zero, one, two or more) solutions $a$. Then to plot them, or visualize via a Table. This is done straightforwardly via Mathematica; however, I want to achieve it via Matlab, if easily possible. The code that I wrote upto now is

clear all
k=[0:0.1:pi]
x=[0:0.1:pi]
f=sin(k+a)-x*cos(k);
for i=1:3
    vpasolve(f,a,'random',true)
end

However, it doesn't get me any closer. Thanks.

2

There are 2 best solutions below

9
On BEST ANSWER

You may want to try something like this:

clear all
syms a
for k=0:0.1:pi
  for x=0:0.1:pi
    f = sin(k+a) - x*cos(x);
    vpasolve(f,a)
  end
end

EDIT: The following code reflects most of the discussion in the comments. It shows how to compute all solutions symbolically and stores them in a cell array:

syms a
k = sym(0:0.1:pi);
x = sym(0:0.1:pi);
T = cell(length(k),length(x));
for m=1:length(k)
  for n=1:length(x)
    if x(n) * cos(x) <= 1
      f = sin(k(m)+a) - x(n)*cos(x(n));
      sol = solve(f,a);
      T{m,n} = sol;
    end
  end
end

The entry of the array that are not assigned in the loop contain empty matrices, which are detected in further processing by calling isempty.

1
On

You get the solutions $$ a=-k+\arcsin(x\cos(k))+2\pi n $$ and $$ a=-k+\pi-\arcsin(x\cos(k))+2\pi n $$ where $n$ has to be adjusted so that $0\le a<2\pi$. The additional restriction $a<\pi$ is then also a restriction on $k$ and $x$, that is, not all combinations might provide solutions.