fzero for a loop of functions

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 Commenti

Density, Viscosity, v?
Filip Jackowski
Filip Jackowski il 6 Mar 2019
Modificato: Filip Jackowski il 6 Mar 2019
My apologies. Density and Viscosity are external functions, both solely dependent on "T" in this case. "v" is the variable I'm trying to "zero" for.
function [rho]=Density(T)
rho= 101300./(287.*T);
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;
T?
T is the vector specified in the original code as:
T = [213.15:5:333.15]

Accedi per commentare.

 Risposta accettata

madhan ravi
madhan ravi il 6 Mar 2019
Modificato: madhan ravi il 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 Commenti

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!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by