# How to feed additional variables into fsolve function

25 views (last 30 days)
Matthew Campbell on 2 Apr 2021
Hello,
I have a 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];
options = optimoptions('fsolve','Display','iter');
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.
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:)
x0 = [150,0.045,7.5];
[out,fval] = fsolve(fun,x0,options)

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

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

R2019b

### Community Treasure Hunt

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

Start Hunting!