MATLAB: Numerical Solution

finding roots

Hi everybody,
I have an equation which is:
i*q=k*sin(q)
I need to use the matlab to find its roots. I want to set a specific values of k like (0.25, 0.5, 1, 2, 3) and find the values of q each time. Thanks in advance folks.

Best Answer

Ok, your "q" will have an imaginary component, so you cannot use fzero directly. You can, however, solve it using fsolve, where you treat q as a 2-element vector containing the real and imaginary parts.
k = 4;
f = @(q) 1i.*[q(1) + 1i*q(2)]-k.*sin([q(1) + 1i*q(2)]);
x0 = [2; 0.5]
fsolve(@(q) norm(f(q)), x0)
This finds a zero at roughly [3; -0.7]. But since there are multiple minima, the result will depend on the initial conditions.
Note that you can make a plot of f, to visually see roughly where the zeros are. This will help you choose initial conditions.
k = 4;
f = @(q1,q2) 1i.*[q1 + 1i*q2]-k.*sin([q1 + 1i*q2]);
[Q1,Q2] = ndgrid(-10:.2:10);
surf(Q1,Q2, log(abs(f(Q1,Q2))));