Using fsolve with type double
이전 댓글 표시
FSOLVE requires all values returned by functions to be of data type double.
Hi, in my MATLAB code there is a function calcX2 and in the main it solves system of 2 nonlinear equations. I defined all variables as syms.
The function is the following;
sumenergy=(((tetha_1)*(Cp_1)*((z(1))-(T_1)))+ ((tetha_2)*(Cp_2)*((z(1))-(T_2)))+((tetha_3)*(Cp_3)*((z(1))-(T_3))));
sumenergy2= (delta_H_T_ref+ (delta_Cp*(z(1)-T_ref)));
F=zeros
for i=1:2
F=[ (U_A*(T_a-z(1))/FA_0)-((z(2)-X_1)*sumenergy2)-(sumenergy);
(V_2*((k_1)*exp(act_energy_per_R*((1/T_k_1)-(1/z(1)))))*CA_0*(1-z(2))^2)-(FA_0*(z(2)-X_1)) ]
end
the main:
func=@calcX2;
z0=[0 0];
conversion= fsolve(func, z0)
But, the code didn't work and it gives error: FSOLVE requires all values returned by functions to be of data type double. How can I eliminate this error? What does this mean?
댓글 수: 2
Stephen23
2020년 5월 26일
Original question by Özgü Dönmez retrieved from Google Cache:
"FSOLVE requires all values returned by functions to be of data type double"
Hi, in my MATLAB code there is a function calcX2 and in the main it solves system of 2 nonlinear equations. I defined all variables as syms.
The function is the following;
sumenergy=(((tetha_1)*(Cp_1)*((z(1))-(T_1)))+ ((tetha_2)*(Cp_2)*((z(1))-(T_2)))+((tetha_3)*(Cp_3)*((z(1))-(T_3))));
sumenergy2= (delta_H_T_ref+ (delta_Cp*(z(1)-T_ref)));
F=zeros
for i=1:2
F=[ (U_A*(T_a-z(1))/FA_0)-((z(2)-X_1)*sumenergy2)-(sumenergy);
(V_2*((k_1)*exp(act_energy_per_R*((1/T_k_1)-(1/z(1)))))*CA_0*(1-z(2))^2)-(FA_0*(z(2)-X_1)) ]
end
the main:
func=@calcX2;
z0=[0 0];
conversion= fsolve(func, z0)
But, the code didn't work and it gives error: FSOLVE requires all values returned by functions to be of data type double. How can I eliminate this error? What does this mean?
Ameer Hamza
2020년 5월 26일
Özgü, how is deleting the question is going to help you in any way? The code in your question is already part of my answer. The only thing it accomplishes is that you are less likely to get a response on any new question.
답변 (1개)
Ameer Hamza
2020년 5월 25일
0 개 추천
To use a symbolic equation with fsolve(), first convert it to a floating-point function handle. See matlabFunction(): https://www.mathworks.com/help/releases/R2020a/symbolic/matlabfunction.html
댓글 수: 3
Ameer Hamza
2020년 5월 25일
Can you show your code?
Ameer Hamza
2020년 5월 25일
Create function like this
function F=calcX2() % z was not being used
syms T X_2
z=[T, X_2];
syms tetha_1 tetha_2 tetha_3 Cp_1 Cp_2 Cp_3 T_1 T_2 T_3 X_1 T_ref T_ad U_A T_a CA_0 v_0 FA_0 V_2 k
syms k_1 act_energy_per_R T_k_1
V_2=0.1;
X_1=0.2;
T_1=300.0;
T_2=300.0;
T_3=300.0;
T_ref=298.0;
Cp_1=10.0;
Cp_2=10.0;
Cp_3=20.0;
tetha_1=1.0;
tetha_2=1.0;
tetha_3=2.0;
delta_Cp=(2*Cp_3)-(Cp_2)-(Cp_1);
CA_0= 10^3;
v_0=0.4;
FA_0=(CA_0)*(v_0);
U_A= 5*10^3;
T_a=350.0;
T_ad=350.0;
delta_H_T_ref=-8540
V_1=1.0;
act_energy_per_R=2663.75
T_k_1=300.0;
k_1=(v_0*X_1)/(V_1*CA_0*(1-X_1)^2)
k=(k_1)*exp(act_energy_per_R*((1/T_k_1)-(1/z(1))));
sumenergy=(((tetha_1)*(Cp_1)*((z(1))-(T_1)))+ ((tetha_2)*(Cp_2)*((z(1))-(T_2)))+((tetha_3)*(Cp_3)*((z(1))-(T_3))));
sumenergy2= (delta_H_T_ref+ (delta_Cp*(z(1)-T_ref)));
F=zeros
for i=1:2
F=[ (U_A*(T_a-z(1))/FA_0)-((z(2)-X_1)*sumenergy2)-(sumenergy);
(V_2*((k_1)*exp(act_energy_per_R*((1/T_k_1)-(1/z(1)))))*CA_0*(1-z(2))^2)-(FA_0*(z(2)-X_1)) ]
end
F = matlabFunction(F, 'Vars', {[T, X_2]});
end
and in the script write
func = calcX2();
z0=[0,0];
conversion = fsolve(func, z0)
Ameer Hamza
2020년 5월 25일
Because inside the function, you have a line
z = [T, X_2];
which overwrite the value of 'z' no matter what you input, so I remove it.
카테고리
도움말 센터 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!