fzero for a loop of functions

12 views (last 30 days)
What I'm trying to do is frustratingly simple. I'm trying to use the fzero function to find a root for the function below, at each value of "n" from 1 to 25. I'm currently running fzero for the function below, then manually changing the value of "n" to 2, saving the .m file, fzero'ing again, and so forth. I'm looking for a cleaner way to do this.
*As a tidbit of extra information that may be relevant, fzero only works when given an input of at least 46 in the form "fzero(@TerminalV,46). anything below that returns a "NaN".
function fx = TerminalV(v)
A = pi()*(0.15)^2/4;
T = [213.15:5:333.15];
for n=1;
fx = 9.8*0.5*2./(Density(T(n)).*A)-(v.^2).*(24./((Density(T(n)).*v.*0.15)./viscosity(T(n)))...;
+6./(1+(Density(T(n)).*v.*0.15./viscosity(T(n))).^0.5)+0.04);
end
end
  4 Comments
Filip Jackowski
Filip Jackowski on 6 Mar 2019
T is the vector specified in the original code as:
T = [213.15:5:333.15]

Sign in to comment.

Accepted Answer

madhan ravi
madhan ravi on 6 Mar 2019
Edited: madhan ravi on 6 Mar 2019
%------------------------------ CODES
T = 213.15:5:333.15;
Results=zeros(25,1);
for n=1:25
Results(n)=fzero(@(v)TerminalV(v,T(n)),[50 150]);
end
%------------------------------ FUNCTIONS
function fx = TerminalV(v,T)
A = pi()*(0.15)^2/4;
fx = 9.8*0.5*2./(Density(T).*A)-(v.^2).*(24./((Density(T).*v.*0.15)./viscosity(T))...
+6./(1+(Density(T).*v.*0.15./viscosity(T)).^0.5)+0.04);
end
function rho = Density(T)
rho= 101300./(287.*T);
end
function mu = viscosity(T)
%b1-b4 are all empirical constants
b1=2.156954157e-14;
b2=-5.332634033e-11;
b3=7.477905983e-8;
b4=2.527878788e-7;
mu = b1.*(T.^3)+b2.*(T.^2)+b3.*T+b4;
end
  2 Comments
Filip Jackowski
Filip Jackowski on 6 Mar 2019
Brilliant. Thank you so much. Could you explain why its necessecary to put the @(v) in front of the TerminalV, and why it doesnt work with just @TerminalV? Also if you would be so kind as to point me towards some resource that explains how to use fzero for functions with multiple inputs, that would be greatly appreciated. Thanks again!

Sign in to comment.

More Answers (0)

Categories

Find more on Problem-Based Optimization Setup in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!