solving non linear equation using fsolve

1 visualización (últimos 30 días)
Shubham
Shubham el 2 de Feb. de 2021
Comentada: Walter Roberson el 2 de Feb. de 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 comentario
Matt J
Matt J el 2 de Feb. de 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).

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 2 de Feb. de 2021
Editada: Matt J el 2 de Feb. de 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 comentarios
Shubham
Shubham el 2 de Feb. de 2021
Editada: Shubham el 2 de Feb. de 2021
%%%% 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 el 2 de Feb. de 2021
Matt already told you what the problem is and how to fix it.

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by