Nonlinear simultaneous equation solution with a syms function.

4 views (last 30 days)
지홍 윤
지홍 윤 on 27 Nov 2021
Commented: 지홍 윤 on 27 Nov 2021
I try to solve a nonlinear simultaneous equation using the function with 'syms'.
The function created by 'syms' was converted to an anonymous function using 'matlabFunction'
So we tried to solve the nonlinear simultaneous equation using 'fsolve' by making two equations and two variables
But It does not proceed due to an error called 'Nonscalarrays of function hands are not allowed; use cell arrays insted'.
I guess, for example, there are function expressed about syms,
f1 = x^2 + y + 2
f2 = log(x) + y^2
These is changed
f1_ = x(1)^2 + x(2) + 2
f2_ = log(x(1)) + x(2)^2
but I don’t know how to change
If there is a solution, please let me know.
Attached is the code below.
%%%%%%%%%%%%%%%
clear all
%%%%%%%%%%%%% Declaration ofvariables%%%%%%%%%%%%%%
x_s=cell(2,1);
R=8.3146261815324;
H_1=13423;
Tm_1=1356.35;
H_2=17936;
Tm_2=1728.15;
al_1=14259;
bl_1=0.45;
al_2=0;
bl_2=0;
as_1=6877.12;
bs_1=4.6;
as_2=-2450.1;
bs_2=1.87;
syms dG_1 dG_2 T x dG_ex_l dG_ex_s dG_l(x,T) dG_s(x,T) x_ x_0 x_1 x_2
dG_1=H_1*((Tm_1-T)./Tm_1);
dG_2=H_2*((Tm_2-T)./Tm_2);
dG_ex_l=(1-x)*x*(al_1+bl_1*T)+(1-x)*x*(al_2+bl_2*T)*(1-2*x);
dG_ex_s=(1-x)*x*(as_1+bs_1*T)+(1-x)*x*(as_2+bs_2*T)*(1-2*x);
dG_l=x*dG_2+R*T*((1-x)*log(1-x)+x*log(x))+dG_ex_l;
dG_s=-(1-x)*dG_1+R*T*((1-x)*log(1-x)+x*log(x))+dG_ex_s;
T_aaa=1300:10:1800;
Tn=T_aaa(10);
dG_l_const_T=subs(dG_l,[x, T], [x_, Tn]);
dG_s_const_T=subs(dG_s,[x, T], [x_, Tn]);
pre_f=dG_s_const_T-dG_l_const_T;
f=matlabFunction(pre_f);
x_0= fzero(f,[0 1]);%%%%%%%fsolve initial Value
dG_l_f1=subs(dG_l,[x, T], [x_1, Tn]);
dG_s_f1=subs(dG_s,[x, T], [x_2, Tn]);
Diff_dG_l_f1 = diff(dG_l_f1,x_1);
Diff_dG_s_f1 = diff(dG_s_f1,x_2);
%%%%%%%%%%% 1st Function %%%%%%%%%%%%%
pre_f1=Diff_dG_s_f1-Diff_dG_l_f1;
f1=matlabFunction(pre_f1); %%%%%%%% Change x_1 to x_s(1), x_2 to x_s(2)
%%%%%%%%%%% 2nd function °%%%%%%%%%%%%%
pre_f2 = Diff_dG_l_f1*(x_2-x_1)-dG_s_f1+dG_l_f1;
f2=matlabFunction(pre_f2);%%%%%%%% Change x_1 to x_s(1), x_2 to x_s(2)
F1_F2=@(x_1, x_2)[f1;f2]; %%%%%%%%%%%%%%%Change F1_F2=@(x_s)[f1;f2];
x_s = fsolve(F1_F2,x_0);% occur error

Accepted Answer

Walter Roberson
Walter Roberson on 27 Nov 2021
F1_F2=@(x_1, x_2)[f1(x_1,x_2);f2(x_1,x_2)]; %%%%%%%%%%%%%%%Change F1_F2=@(x_s)[f1;f2];
Or you could have computed:
F1_F2 = matlabFunction([pre_f1; pre_f2]);
without building f1 or f2
  1 Comment
지홍 윤
지홍 윤 on 27 Nov 2021
Thank you for your answer.
I solved the problem by adding the following code.
F1_F2_s = @(x_s) F1_F2(x_s(1),x_s(2));

Sign in to comment.

More Answers (0)

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!