Solution of Matlab Nonlinear Convex Equations with syms Function

Asked 2 years ago, Updated 2 years ago, 80 views

Create a function using syms and use that function

You are trying to solve a non-linear simultaneous equation.

A function made of syms is converted into an anonymous function using matlabFunction

We tried to solve the nonlinear equations using fsolve by making two equations and two variables

'Non-collar arrangements with function handles are not allowed. Please use a cell array instead.' does not proceed.

Functions expressed in syms

f1 = x2 + y + 2

2

f2 = log(x) + y2

2

Functions such as

f1_ = x(1)2 + x(2) + 2

2

f2_ = log(x(1)) + x(2)2

2

I think I need to change it to the form, so please let me know if there is a solution.

Attached is the code below.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all

%%%%%%% variable declaration%%%%%%%%

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;

%%%%%%% variable declaration end %%%%%%%

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,[01]);%%%%%%%fsolve Initial Value Calculation   


  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);

  %%%%%%%%%%% The first of the two equations, %%%%%%%%%%%%%%

  pre_f1=Diff_dG_s_f1-Diff_dG_l_f1; 

  f1=matlabFunction(pre_f1); %%%%%%%%%x_1 must be replaced by x_s(1) and x_2 by x_s(2)

 %%%%%%%%%%% The second of the two equations, %%%%%%%%%%%%%

 pre_f2 = Diff_dG_l_f1*(x_2-x_1)-dG_s_f1+dG_l_f1; 
 f2=matlabFunction(pre_f2);%%%%%%%%%%x_1 must be replaced by x_s(1) and x_2 by x_s(2)


 F1_F2=@(x_1, x_2)[f1;f2];

 x_s = fsolve(F1_F2,x_0);% occur error 

matlab

2022-09-20 14:23

1 Answers

F1_F2_s = @(x_s) F1_F2(x_s(1),x_s(2));

I solved it by adding the above code.


2022-09-20 14:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.