MATLAB: Solve the equation with variable

function solve

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

Best 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.)