MATLAB Answers

How to feed additional variables into fsolve function

4 views (last 30 days)
Hello,
I have a function F = quad(x,REF)
function F = quad(x,REF)
F(1) = x(3)/((T(1)/x(1))-1) + log(x(2)) - log(dv(1))
F(2) = x(3)/((T(2)/x(1))-1) + log(x(2)) - log(dv(2))
F(3) = x(3)/((T(3)/x(1))-1) + log(x(2)) - log(dv(3))
F(4) = x(3)/((T(4)/x(1))-1) + log(x(2)) - log(dv(4))
F(5) = x(3)/((T(5)/x(1))-1) + log(x(2)) - log(dv(5))
F(6) = x(3)/((T(6)/x(1))-1) + log(x(2)) - log(dv(6))
F(7) = x(3)/((T(7)/x(1))-1) + log(x(2)) - log(dv(7))
F(8) = x(3)/((T(8)/x(1))-1) + log(x(2)) - log(dv(8))
end
I'd like to feed different dv and T values from the main program and solve. I've tried the below code from another post (modified and played around a bit to no avail). How can I feed dv and T values into the function quad and then use fsolve?
REF = [x,dv,T];
f =@(x) quad(x,REF);
options = optimoptions('fsolve','Display','iter');
% fun(dv,T) = @quad;
x0 = [150,0.045,7.5];
[out,fval] = fsolve(f,x0,options)
This works fine when the T and dv are already in the function quad, but I don't want to manually copy and paste new values of dv and T in.
function F = quad(x)
dv = [3610.30 2040.80 1203.70 483.04 106.03 15.54 3.39 1.97]
T = [-40 -35 -30 -20 0 40 100 135] + 273.15
F(1) = x(3)/((T(1)/x(1))-1) + log(x(2)) - log(dv(1))
F(2) = x(3)/((T(2)/x(1))-1) + log(x(2)) - log(dv(2))
F(3) = x(3)/((T(3)/x(1))-1) + log(x(2)) - log(dv(3))
F(4) = x(3)/((T(4)/x(1))-1) + log(x(2)) - log(dv(4))
F(5) = x(3)/((T(5)/x(1))-1) + log(x(2)) - log(dv(5))
F(6) = x(3)/((T(6)/x(1))-1) + log(x(2)) - log(dv(6))
F(7) = x(3)/((T(7)/x(1))-1) + log(x(2)) - log(dv(7))
F(8) = x(3)/((T(8)/x(1))-1) + log(x(2)) - log(dv(8))
end
(CALLED FROM THE MAIN AS:)
fun = @quad;
x0 = [150,0.045,7.5];
[out,fval] = fsolve(fun,x0,options)

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 3 Apr 2021
dv = [3610.30,2040.80,1203.70,483.04,106.03,15.54,3.39,1.97];
T = [-40,-35,-30,-20,0,40,100,135] + 273.1;
f = @(x) myquad(x,dv,T);
x0 = [150,0.045,7.5];
[out,fval] = fsolve(f,x0)
Warning: Trust-region-dogleg algorithm of FSOLVE cannot handle non-square systems; using Levenberg-Marquardt algorithm instead.
No solution found. fsolve stopped because the last step was ineffective. However, the vector of function values is not near zero, as measured by the value of the function tolerance.
out = 1×3
144.8987 0.0415 6.9479
fval = 1×8
0.0407 -0.0012 -0.0232 -0.0576 0.0072 0.0600 0.0089 -0.0349
function F = myquad(x,dv,T)
F(1) = x(3)/((T(1)/x(1))-1) + log(x(2)) - log(dv(1));
F(2) = x(3)/((T(2)/x(1))-1) + log(x(2)) - log(dv(2));
F(3) = x(3)/((T(3)/x(1))-1) + log(x(2)) - log(dv(3));
F(4) = x(3)/((T(4)/x(1))-1) + log(x(2)) - log(dv(4));
F(5) = x(3)/((T(5)/x(1))-1) + log(x(2)) - log(dv(5));
F(6) = x(3)/((T(6)/x(1))-1) + log(x(2)) - log(dv(6));
F(7) = x(3)/((T(7)/x(1))-1) + log(x(2)) - log(dv(7));
F(8) = x(3)/((T(8)/x(1))-1) + log(x(2)) - log(dv(8));
end
  1 Comment
Matthew Campbell
Matthew Campbell on 3 Apr 2021
Thanks Stephen. This was exactly what I was looking for. Thanks also for attaching the link - I knew this information would be somewhere, but I wasn't searching for the right terms (such as "extra parameters").

Sign in to comment.

More Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 2 Apr 2021
Edited: Sulaymon Eshkabilov on 2 Apr 2021
Here is a combined and a bit simplified solution:
options = optimoptions('fsolve','Display','iter');
x0 = [150,0.045,7.5];
dv = [3610.30 2040.80 1203.70 483.04 106.03 15.54 3.39 1.97];
T = [-40 -35 -30 -20 0 40 100 135] + 273.15;
[out,fval] = fsolve(@(x)([ x(3)/((T(1)/x(1))-1) + log(x(2)) - log(dv(1));
x(3)/((T(2)/x(1))-1) + log(x(2)) - log(dv(2));
x(3)/((T(3)/x(1))-1) + log(x(2)) - log(dv(3));
x(3)/((T(4)/x(1))-1) + log(x(2)) - log(dv(4));
x(3)/((T(5)/x(1))-1) + log(x(2)) - log(dv(5));
x(3)/((T(6)/x(1))-1) + log(x(2)) - log(dv(6));
x(3)/((T(7)/x(1))-1) + log(x(2)) - log(dv(7));
x(3)/((T(8)/x(1))-1) + log(x(2)) - log(dv(8));]), x0,options)
  1 Comment
Matthew Campbell
Matthew Campbell on 3 Apr 2021
Hi Sulaymon. Thanks for the reply. This also works when you are not looking to call a separate function.

Sign in to comment.

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!