Solve the equation with variable

function x = enthalpy(h)
eqn=@(x,h) 38.0657.*(( -x.*((3*0.605719400e-7)./x.^4 - 17.275266575 + (2*-0.210274769e-4)./x.^3 + (-0.158860716E-3)./x.^2 - (2.490888032)./x - (3*(-0.195363420E-3).*x.^(1/2))/2 + ((0.791309509)*(25.36365)*exp(-(25.36365).*x))./(exp(-(25.36365).*x) - 1) + ((0.212236768)*(16.90741)*exp(-(16.90741).*x))./(exp(-(16.90741).*x) - 1) - ((-0.197938904)*(87.31279)*exp((87.31279).*x))./(exp((87.31279).*x) + 2/3)))+1)./x - h;
x = solve(@(x,h)eqn==0,x,options)
end
h is changable. functiıon is not giving a answer. how can ı solve this function

 Accepted Answer

A bit of editing is in order, and a different optimisation function.
Try this:
eqn=@(x,h) 38.0657.*(( -x.*((3*0.605719400e-7)./x.^4 - 17.275266575 + (2*-0.210274769e-4)./x.^3 + (-0.158860716E-3)./x.^2 - (2.490888032)./x - (3*(-0.195363420E-3).*x.^(1/2))/2 + ((0.791309509)*(25.36365)*exp(-(25.36365).*x))./(exp(-(25.36365).*x) - 1) + ((0.212236768)*(16.90741)*exp(-(16.90741).*x))./(exp(-(16.90741).*x) - 1) - ((-0.197938904)*(87.31279)*exp((87.31279).*x))./(exp((87.31279).*x) + 2/3)))+1)./x - h;
x0 = 1;
x = fminsearch(@(x)norm(eqn(x,h)),x0)
There are several functions that would likely work, including fminunc and others. The solve function is only for symbolic functions, so it is not appropriate here.
Since you want to solve for ‘x’ that is the only varialbe the optimisation function needs to know about, so while both values need to be passed to the function, only ‘x’ is important to the optimisation. You are finding the minimum between the function and ‘h’, and the norm function optimises that. (It is the easiest to use here.)

4 Comments

thanks so much for your answer :) it's working
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
star strider I have notice that , ı have to click 'RUN' button four times or more to obtain correct value;
Look T= Temp
R =8.314510; % Joule / ( mol * Kelvin)
Mass =28.9669; % 28,9586; g / mol
Temp=700
P=0.5
rho =( P * 1000)./( R.* T) ;
Tj =132.6312; %Kelvin (Maxcondentherm temperature)
rhoj =10.4477; %mol/dm3 (Maxcondentherm density)
Pj =3.78502; %MPa (Maxcondentherm pressure)
N1=0.605719400e-7; N2=-0.210274769e-4; N3=-0.158860716E-3;
N4=-13.841928076; N5=17.275266575; N6=-0.195363420E-3;
N7=2.490888032; N8=0.791309509; N9=0.212236768;
N10=-0.197938904; N11=25.36365; N12=16.90741;
N13=87.31279;
x=Tj./Temp;
y=rho./rhoj;
a0 = log(y) + N1.*x.^(-3) + N2.* x.^(-2) + N3.* x.^(-1) + N4 + N5.* x + ...
N6.* x.^1.5 + N7.* log(x) + N8.* log(1 - exp(-N11.* x)) + N9.* log(1 - exp(-N12.* x)) + ...
N10.* log((2/3) + exp(N13.* x));
x_da0_dx = -x.*((3*N1)./x.^4 - N5 + (2*N2)./x.^3 + N3./x.^2 - N7./x - (3*N6.*x.^(1/2))/2 + ...
(N8*N11*exp(-N11.*x))./(exp(-N11.*x) - 1) + (N9*N12*exp(-N12.*x))./(exp(-N12.*x) - 1) - ...
(N10*N13*exp(N13.*x))./(exp(N13.*x) + 2/3));
s =(R.*(-a0-x_da0_dx+1))./Mass % kj / kg Kelvin % WE ARE OBTAINING FIRST ENTROPY VALUE WITH THIS FUNCTION , BUT WITH FIRST ENTROPY VALUE WE COULDNT OBTAIN CORRECT TEMPERATURE VALUE BY USING "FMINSEARCH" FUNCTION.
WE HAVE TO CLICK four times "RUN" BUTTON TO GET CORRECT TEMPERATURE VALUE
eqn =@(x,s,P,R,Mass,Tj,rhoj,N1,N2,N3,N4,N5,N6,N7,N8,N9,N10,N11,N12,N13)(R/Mass)*(-(log(( x * P * 1000)./( R* Tj*rhoj)) + N1.*x.^(-3) + N2.* x.^(-2) + N3.* x.^(-1) + N4 + N5.* x + ...
N6.* x.^1.5 + N7.* log(x) + N8.* log(1 - exp(-N11.* x)) + N9.* log(1 - exp(-N12.* x)) + ...
N10.* log((2/3) + exp(N13.* x)))-(-x.*((3*N1)./x.^4 - N5 + (2*N2)./x.^3 + N3./x.^2 - N7./x - (3*N6.*x.^(1/2))/2 + ...
(N8*N11*exp(-N11.*x))./(exp(-N11.*x) - 1) + (N9*N12*exp(-N12.*x))./(exp(-N12.*x) - 1) - ...
(N10*N13*exp(N13.*x))./(exp(N13.*x) + 2/3)))+1)-s;
options = optimset('Display','iter','MaxFunEvals',2e3)
x0=1
x = fminsearch(@(x)norm(eqn(x,s,P,R,Mass,Tj,rhoj,N1,N2,N3,N4,N5,N6,N7,N8,N9,N10,N11,N12,N13)),x0,options)
T=Tj./x
eqn(x,s,P,R,Mass,Tj,rhoj,N1,N2,N3,N4,N5,N6,N7,N8,N9,N10,N11,N12,N13) % to look to error value
I do not understand what the problem is, or what the ‘correct value’ is. Nonlinear parameter estimation routines are very sensitive to the starting value (here ‘x0’). Experiment with different values for it to get different results. I get the same result for ‘x’ (0.1895) with several different solvers (for example fminsearch, fminunc) in R2020a with the code you posted.

Sign in to comment.

More Answers (2)

onur karakurt
onur karakurt on 1 May 2020
star strider I have notice that , ı have to click 'RUN' button four times or more to obtain correct value;
Look T= Temp
R =8.314510; % Joule / ( mol * Kelvin)
Mass =28.9669; % 28,9586; g / mol
Temp=700
P=0.5
rho =( P * 1000)./( R.* T) ;
Tj =132.6312; %Kelvin (Maxcondentherm temperature)
rhoj =10.4477; %mol/dm3 (Maxcondentherm density)
Pj =3.78502; %MPa (Maxcondentherm pressure)
N1=0.605719400e-7; N2=-0.210274769e-4; N3=-0.158860716E-3;
N4=-13.841928076; N5=17.275266575; N6=-0.195363420E-3;
N7=2.490888032; N8=0.791309509; N9=0.212236768;
N10=-0.197938904; N11=25.36365; N12=16.90741;
N13=87.31279;
x=Tj./Temp;
y=rho./rhoj;
a0 = log(y) + N1.*x.^(-3) + N2.* x.^(-2) + N3.* x.^(-1) + N4 + N5.* x + ...
N6.* x.^1.5 + N7.* log(x) + N8.* log(1 - exp(-N11.* x)) + N9.* log(1 - exp(-N12.* x)) + ...
N10.* log((2/3) + exp(N13.* x));
x_da0_dx = -x.*((3*N1)./x.^4 - N5 + (2*N2)./x.^3 + N3./x.^2 - N7./x - (3*N6.*x.^(1/2))/2 + ...
(N8*N11*exp(-N11.*x))./(exp(-N11.*x) - 1) + (N9*N12*exp(-N12.*x))./(exp(-N12.*x) - 1) - ...
(N10*N13*exp(N13.*x))./(exp(N13.*x) + 2/3));
s =(R.*(-a0-x_da0_dx+1))./Mass % kj / kg Kelvin % WE ARE OBTAINING FIRST ENTROPY VALUE WITH THIS FUNCTION , BUT WITH FIRST ENTROPY VALUE WE COULDNT OBTAIN CORRECT TEMPERATURE VALUE BY USING "FMINSEARCH" FUNCTION.
WE HAVE TO CLICK four times "RUN" BUTTON TO GET CORRECT TEMPERATURE VALUE
eqn =@(x,s,P,R,Mass,Tj,rhoj,N1,N2,N3,N4,N5,N6,N7,N8,N9,N10,N11,N12,N13)(R/Mass)*(-(log(( x * P * 1000)./( R* Tj*rhoj)) + N1.*x.^(-3) + N2.* x.^(-2) + N3.* x.^(-1) + N4 + N5.* x + ...
N6.* x.^1.5 + N7.* log(x) + N8.* log(1 - exp(-N11.* x)) + N9.* log(1 - exp(-N12.* x)) + ...
N10.* log((2/3) + exp(N13.* x)))-(-x.*((3*N1)./x.^4 - N5 + (2*N2)./x.^3 + N3./x.^2 - N7./x - (3*N6.*x.^(1/2))/2 + ...
(N8*N11*exp(-N11.*x))./(exp(-N11.*x) - 1) + (N9*N12*exp(-N12.*x))./(exp(-N12.*x) - 1) - ...
(N10*N13*exp(N13.*x))./(exp(N13.*x) + 2/3)))+1)-s;
options = optimset('Display','iter','MaxFunEvals',2e3)
x0=1
x = fminsearch(@(x)norm(eqn(x,s,P,R,Mass,Tj,rhoj,N1,N2,N3,N4,N5,N6,N7,N8,N9,N10,N11,N12,N13)),x0,options)
T=Tj./x
eqn(x,s,P,R,Mass,Tj,rhoj,N1,N2,N3,N4,N5,N6,N7,N8,N9,N10,N11,N12,N13) % to look to error value

Community Treasure Hunt

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

Start Hunting!