solving non linear equation using fsolve

9 views (last 30 days)
Shubham
Shubham on 2 Feb 2021
Commented: Walter Roberson on 2 Feb 2021
function F = root2d(phi)
F = zeros(2,1);
F(1) = (pi*sin((pi*(phi(1) + theta_n))/180)*cos((pi*phi(2))/180)*(cos((pi*theta_n)/180) + tan((pi*i)/180)*tan((pi*theta_i)/180)))/(180*sin((pi*phi(1))/180)*(cos((pi*(phi(1) + theta_n))/180)*cos((phi(2)*pi)/180) + sin((phi(2)*pi)/180)*tan((theta_i*pi)/180))^2) - (pi*cos((pi*phi(1))/180)*(cos((pi*theta_n)/180) + tan((pi*i)/180)*tan((pi*theta_i)/180)))/(180*sin((phi(1)*pi)/180)^2*(cos((pi*(phi(1) + theta_n))/180)*cos((pi*phi(2))/180) + sin((pi*phi(2))/180)*tan((pi*theta_i)/180)));
F(2) = ((cos((pi*theta_n)/180) + tan((pi*i)/180)*tan((pi*theta_i)/180))*((pi*cos((pi*(phi(1) + theta_n))/180)*sin((pi*phi(2))/180))/180 - (pi*cos((pi*phi(2))/180)*tan((pi*theta_i)/180))/180))/(sin((pi*phi(1))/180)*(cos((pi*(phi(1) + theta_n))/180)*cos((phi(2)*pi)/180) + sin((phi(2)*pi)/180)*tan((theta_i*pi)/180))^2);
end
beta_a = 10;
alpha_n = 10 ;
eta= 18;
i = 18;
theta_i = 10;
theta_n = 10 ;
fun = @root2d;
phi0 = [0,0];
phi = fsolve(fun,phi0);
phi_n = phi(1);
phi_i = phi(2);
I am getting error :
Unrecognized function or variable 'theta_n'.
Error in root2d (line 4)
F(1) = (pi*sin((pi*(phi(1) + theta_n))/180)*cos((pi*phi(2))/180)*(cos((pi*theta_n)/180) +
tan((pi*i)/180)*tan((pi*theta_i)/180)))/(180*sin((pi*phi(1))/180)*(cos((pi*(phi(1) +
theta_n))/180)*cos((phi(2)*pi)/180) + sin((phi(2)*pi)/180)*tan((theta_i*pi)/180))^2) -
(pi*cos((pi*phi(1))/180)*(cos((pi*theta_n)/180) +
tan((pi*i)/180)*tan((pi*theta_i)/180)))/(180*sin((phi(1)*pi)/180)^2*(cos((pi*(phi(1) +
theta_n))/180)*cos((pi*phi(2))/180) + sin((pi*phi(2))/180)*tan((pi*theta_i)/180)));
Error in fsolve (line 258)
fuser = feval(funfcn{3},x,varargin{:});
Error in recent (line 12)
phi = fsolve(fun,phi0);
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
  1 Comment
Matt J
Matt J on 2 Feb 2021
Your code would be more readable (and hence easier to debug) if you used cosd(x) and sind(x) instead of cos(pi*x/180) and sin(pi*x/180).

Sign in to comment.

Accepted Answer

Matt J
Matt J on 2 Feb 2021
Edited: Matt J on 2 Feb 2021
You are not passing theta_n and other parameters to root2d(). One fix is to make root2d a Nested Function. Also, you must choose a different initial point. phi0 = [10,10] worked for me.
function solve_it()
beta_a = 10;
alpha_n = 10 ;
eta= 18;
i = 18;
theta_i = 10;
theta_n = 10 ;
fun = @root2d;
phi0 = [10,10];
phi = fsolve(fun,phi0);
phi_n = phi(1),
phi_i = phi(2),
function F = root2d(phi)
F = zeros(2,1);
F(1) = (pi*sin((pi*(phi(1) + theta_n))/180)*cos((pi*phi(2))/180)*(cos((pi*theta_n)/180) + tan((pi*i)/180)*tan((pi*theta_i)/180)))/(180*sin((pi*phi(1))/180)*(cos((pi*(phi(1) + theta_n))/180)*cos((phi(2)*pi)/180) + sin((phi(2)*pi)/180)*tan((theta_i*pi)/180))^2) - (pi*cos((pi*phi(1))/180)*(cos((pi*theta_n)/180) + tan((pi*i)/180)*tan((pi*theta_i)/180)))/(180*sin((phi(1)*pi)/180)^2*(cos((pi*(phi(1) + theta_n))/180)*cos((pi*phi(2))/180) + sin((pi*phi(2))/180)*tan((pi*theta_i)/180)));
F(2) = ((cos((pi*theta_n)/180) + tan((pi*i)/180)*tan((pi*theta_i)/180))*((pi*cos((pi*(phi(1) + theta_n))/180)*sin((pi*phi(2))/180))/180 - (pi*cos((pi*phi(2))/180)*tan((pi*theta_i)/180))/180))/(sin((pi*phi(1))/180)*(cos((pi*(phi(1) + theta_n))/180)*cos((phi(2)*pi)/180) + sin((phi(2)*pi)/180)*tan((theta_i*pi)/180))^2);
end
end
  2 Comments
Walter Roberson
Walter Roberson on 2 Feb 2021
Matt already told you what the problem is and how to fix it.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!