solving non linear equation using fsolve

4 次查看(过去 30 天)
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 个评论
Matt J
Matt J 2021-2-2
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).

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2021-2-2
编辑:Matt J 2021-2-2
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 个评论
Shubham
Shubham 2021-2-2
编辑:Shubham 2021-2-2
%%%% this is what I am trying to do, the nested code thing is not working here
syms theta_n theta_i phi_n phi_i i;
N = 5 ;
beta_a = 10;
alpha_n = 10 ;
eta = zeros(1,N) ;
eta(1)= 18;
i = 18;
for k=1:N-1
theta_i = asind(sind(beta_a)*sind(eta(k)));
theta_n = atand(tand(beta_a)*cosd(eta(k)))- alpha_n ;
%% substituting the above values in the two nonlinear eqns and obtaining phi_i and phi_n
fun = @root2d;
phi_0 = [0,0];
phi = fsolve(fun,phi_0);
phi_n = phi(1);
phi_i = phi(2);
eta(k+1) = atand((tand(i)*cosd(phi_n-alpha_n)-cosd(alpha_n)*tand(phi_i))/sind(phi_n));
end
plot(1:N,eta)
%%% This is the function
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
Walter Roberson
Walter Roberson 2021-2-2
Matt already told you what the problem is and how to fix it.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by