fsolve with writting automatically equations
2 views (last 30 days)
Show older comments
In this code I write equations in this form in a for loop:
Dtau(i)+2*x(1)*l(i)*(s(i)^2*(cos(x(2))*cos(alpha_1(i))+sin(x(2))*sin(alpha_1(i)))
and solve the nonlinear system with fsolve for x.
When i add the definition of V_AB(i) to calculate the times for the equations it gives me this error:
Error using inlineeval
Error in inline expression ==> 56.5685424949+2*x(1)*1.0000000000*((0.0021272263)^2)*(cos(x(2))*cos(1.5707963268)+sin(x(2))*sin(1.5707963268))
0.0000000000+2*x(1)*0.7071067812*((0.0021272263)^2)*(cos(x(2))*cos(-0.7853981634)+sin(x(2))*sin(-0.7853981634)) 80.0000000000+2*x(1)*0.7071067812*((0.0021272263)^2)*(cos(x(2))*cos(0.7853981634)+sin(x(2))*sin(0.7853981634))
80.0000000000+2*x(1)*0.7071067812*((0.0021272263)^2)*(cos(x(2))*cos(0.7853981634)+sin(x(2))*sin(0.7853981634)) 0.0000000000+2*x(1)*0.7071067812*((0.0021272263)^2)*(cos(x(2))*cos(-0.7853981634)+sin(x(2))*sin(-0.7853981634))
56.5685424949+2*x(1)*1.0000000000*((0.0021272263)^2)*(cos(x(2))*cos(0.0000000000)+sin(x(2))*sin(0.0000000000))
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
While if i calculate V_AB as a constant, without the index 'i', fsolve works.
The code i wrote is:
%defining constants
k=1.4;
R_gas=287;
T_media_vera=550; %[K]
CL_VERA=(k*R_gas*T_media_vera)^0.5;
s=1/CL_VERA;
V_vera=40; %[m/s]
Beta_vero=pi/4; %[rad]
p = rand(6,2);
q = rand(6,2);
% writing equations in a txt file:
FID = fopen('equations list.txt','w');
F=zeros(length(p),1);
for i=1:length(p)
% lengths
l(i)=((q(i,1)-p(i,1))^2+(q(i,2)-p(i,2))^2)^0.5;
% angles
alpha_1(i)=atan((q(i,2)-p(i,2))/((q(i,1)-p(i,1))));
%velocity
V_AB(i)=V_vera*(cos(alpha_1(i))*cos(Beta_vero)+(sin(alpha_1(i))*sin(Beta_vero)));
% times
tau_f(i)=l(i)/(CL_VERA+V_vera);
tau_b(i)=l(i)/(CL_VERA-V_vera);
% tau_f(i)=l(i)/(CL_VERA+(V_AB(i)));
% tau_b(i)=l(i)/(CL_VERA-(V_AB(i)));
% if i add these two instead of the 2 lines above the code gives me the error!
Dtau(i)=tau_f(i)-tau_b(i);
% printing
formatSpec = '%.10f+2*x(1)*%.10f*((%.10f)^2)*(cos(x(2))*cos(%.10f)+sin(x(2))*sin(%.10f)) \n';
F(i)=fprintf(FID,formatSpec,Dtau(i),l(i),s,alpha_1(i),alpha_1(i));
end
%importing equations and preparing them for fsolve
A=importdata('equations list.txt');
B=transpose(A);
C=cell2mat(B);
%defining problem
problem.objective = C;
problem.x0 = [0,0];
problem.solver = 'fsolve';
%setting tolerances
problem.options = optimoptions('fsolve','MaxIter', ...
4000,'MaxFunEvals',4000,'StepTolerance',1e-16, ...
'FunctionTolerance',1e-16,'OptimalityTolerance',1e-10);
%solving
x = fsolve(problem)
Does anybody know hot to solve this using
% tau_f(i)=l(i)/(CL_VERA+(V_AB(i)));
% tau_b(i)=l(i)/(CL_VERA-(V_AB(i)));
?
Any help would be greatly appreciated.
0 Comments
Accepted Answer
Karim
on 24 Dec 2022
Edited: Karim
on 24 Dec 2022
Please try to avoid asking the same problems in many different questions. This makes it difficult for other people to track tings.
Below you can find the operational code, the easiest solution is to write a function instead of a text file. Later you can call this function to solve the problem.
%defining constants
k=1.4;
R_gas=287;
T_media_vera=550; %[K]
CL_VERA=(k*R_gas*T_media_vera)^0.5;
s=1/CL_VERA;
V_vera=40; %[m/s]
Beta_vero=pi/4; %[rad]
p = rand(6,2);
q = rand(6,2);
% writing equations in a txt file:
FID = fopen('MyFun.m','w');
% first wirte the starting line
fprintf(FID,'function F = MyFun(x)\n');
for i=1:length(p)
% lengths
l(i)=((q(i,1)-p(i,1))^2+(q(i,2)-p(i,2))^2)^0.5;
% angles
alpha_1(i)=atan((q(i,2)-p(i,2))/((q(i,1)-p(i,1))));
%velocity
V_AB(i)=V_vera*(cos(alpha_1(i))*cos(Beta_vero)+(sin(alpha_1(i))*sin(Beta_vero)));
% times
tau_f(i)=l(i)/(CL_VERA+(V_AB(i)));
tau_b(i)=l(i)/(CL_VERA-(V_AB(i)));
Dtau(i)=tau_f(i)-tau_b(i);
% printing
formatSpec = 'F(%i) = %.10f+2*x(1)*%.10f*((%.10f)^2)*(cos(x(2))*cos(%.10f)+sin(x(2))*sin(%.10f)); \n';
fprintf(FID,formatSpec,i,Dtau(i),l(i),s,alpha_1(i),alpha_1(i));
end
% write the 'end' section of the function
fprintf(FID,'end\n');
% close the file
fclose(FID);
% import the file as a text file just to display the result
readlines('MyFun.m')
%defining problem
problem.objective = @MyFun; % here just link to the function file we created
problem.x0 = [0,0];
problem.solver = 'fsolve';
%setting tolerances
problem.options = optimoptions('fsolve','MaxIter', ...
4000,'MaxFunEvals',4000,'StepTolerance',1e-16, ...
'FunctionTolerance',1e-16,'OptimalityTolerance',1e-10,...
'Algorithm','levenberg-marquardt');
%solving
x = fsolve(problem);
% display the solution
x
More Answers (1)
Walter Roberson
on 24 Dec 2022
F(i) = str2func("@(x)" + sprintf(FID,formatSpec,Dtau(i),l(i),s,alpha_1(i),alpha_1(i)));
and leave out the file I/O
Notice that it is sprintf() not fprintf() here. The return value from fprintf() is the number of items converted, not a string or character vector.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!