How to use a fzero function and subfunctions?

Hi,
I am working on a code that involves using an fzero function but I can't figure out how to make it work. I need to find the density of air. This is what I have thus far.
%Parameters
T=250; Tb=340; Bstr=20.5*10^6; b_4=-0.00168785; b_3=-0.0223299;
b_2=-0.1704; b_1=-1.94783; b0=2.16059; b1=-0.0222243; c_3=2.50287;
c_2=-5.52109; c_1=4.29631; c0=1.19665; R=8.314; P=0.01;
tau=T/Tb;
B=Bstr*(b_4*tau^-4+b_3*tau^-3+b_2*tau^-2+b_1*tau^-1+b0*tau^0+b1*tau^1);
C=Bstr^2*(c_3*tau^-3+c_2*tau^-2+c_1*tau^-1+c0*tau^0);
x0=(initalguess);
x=fzero(@func1,x0);
p=0.029*pbar;

5 Comments

what is your function here? where did you define func1
I think I forgot to add it in. Here is the updated version.
function [den] = blackgandy_stasha_01(~,~)
%Parameters
T=250; Tb=340; Bstr=20.5*10^6; b_4=-0.00168785; b_3=-0.0223299;
b_2=-0.1704; b_1=-1.94783; b0=2.16059; b1=-0.0222243; c_3=2.50287;
c_2=-5.52109; c_1=4.29631; c0=1.19665;
%Equation 2
tau=T/Tb;
B=Bstr*(b_4*tau^-4+b_3*tau^-3+b_2*tau^-2+b_1*tau^-1+b0*tau^0+b1*tau^1);
C=Bstr^2*(c_3*tau^-3+c_2*tau^-2+c_1*tau^-1+c0*tau^0);
x0=1;
x=fzero(@(pbar) 1+B*pbar+C*pbar^2,x0)
p=0.029*pbar;
%subfunction
function [] = pbar
% Parameters
T=250; R=8.314; P=0.01;
Z=P/pbar*R*T;
Z=1+B*pbar+C*pbar^2
end
can you please edit your code so that it is clearly viewable? you can edit the code using Code{ } button
I can't find that button but all I am really needing to understand is how to input the equation in the function under %subfunction to get it to be called up to work for the fzero?
This button.
It is on the toolbar in every Question/Answer/Comment box.

Sign in to comment.

 Accepted Answer

Stasha
Stasha on 3 Dec 2013
Edited: Matt J on 3 Dec 2013
%Inputs
%T = Temperature, P = Pressure
T = input('Required temperature in Kelvin')
P = input('Required pressure in MPa')
%Parameters
Tb=340; Bstr=20.5*10^6; b_4=-0.00168785; b_3=-0.0223299;
b_2=-0.1704; b_1=-1.94783; b0=2.16059; b1=-0.0222243; c_3=2.50287;
c_2=-5.52109; c_1=4.29631; c0=1.19665;
%Equation 2
tau=T/Tb;
B=Bstr*(b_4*tau^-4+b_3*tau^-3+b_2*tau^-2+b_1*tau^-1+b0*tau^0+b1*tau^1);
C=Bstr^2*(c_3*tau^-3+c_2*tau^-2+c_1*tau^-1+c0*tau^0);
x0=1;
x=fzero(@(pbar) 1+B*pbar+C*pbar^2,x0)
p=0.029*pbar;
%Parameters
Ts=132.5; pstr=314.3; H=6.1609*10^-6; A1=1.128516; A_5=2.60661; A0=-1.00;
A_1=-0.709661; A_2=0.662534; A_3=-0.197846; A_4=0.00770147; B1=0.465601;
B2=1.26469; B3=-0.511425; B4=0.2746;
%Equation 1
pr=p/pstr;
Tr=T/Ts;
yt=A1*Tr+A_5*Tr^0.5+A0*Tr^0+A_1*Tr^-1+A_2*Tr^-2+A_3*Tr^-3+A_4*Tr^-4;
yp=B1*pr+B2*pr^2+B3*pr^3+B4*pr^4;
y=H*(yt+yp);
end
%subfunction
function [] = pbar
% Parameters
T=250; R=8.314;
Z=P/pbar*R*T;
Z=1+B*pbar+C*pbar^2
end

More Answers (1)

Your code is working but its not giving correct ans because of your initial condition:
%Parameters
T=250;
Tb=340;
Bstr=20.5*10^6;
b_4=-0.00168785;
b_3=-0.0223299;
b_2=-0.1704;
b_1=-1.94783;
b0=2.16059;
b1=-0.0222243;
c_3=2.50287;
c_2=-5.52109;
c_1=4.29631;
c0=1.19665;
%Equation 2
tau=T/Tb;
B=Bstr*(b_4*tau^-4+b_3*tau^-3+b_2*tau^-2+b_1*tau^-1+b0*tau^0+b1*tau^1);
C=Bstr^2*(c_3*tau^-3+c_2*tau^-2+c_1*tau^-1+c0*tau^0);
x0 = 1;
x = fzero(@(pbar)1+B*pbar+C*pbar^2 ,x0) ;

1 Comment

Sixwwwwww, The code you said runs fine is not my complete code. The one above it is and that is the one I'm getting an error on.

Sign in to comment.

Categories

Asked:

on 3 Dec 2013

Commented:

on 3 Dec 2013

Community Treasure Hunt

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

Start Hunting!