fsolve with writting automatically equations
古いコメントを表示
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.
採用された回答
その他の回答 (1 件)
Walter Roberson
2022 年 12 月 24 日
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.
カテゴリ
ヘルプ センター および File Exchange で Programming についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!