solving non linear equation using fsolve
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Shubham
am 2 Feb. 2021
Kommentiert: Walter Roberson
am 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 Kommentar
Matt J
am 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).
Akzeptierte Antwort
Matt J
am 2 Feb. 2021
Bearbeitet: Matt J
am 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 Kommentare
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!