Using fzero function to solve a nonlinear equation with one input
显示 更早的评论
Hi,
I am trying to solve this non-linear equation for alpha using a range of values of X (0-100) [so that I can plot alpha vs X].
function y=equations(alpha)
[D, A, R] = geom();
[n, m] = setglbl();
[X] = setX();
Ag = (R.^2)*(pi-alpha+0.5*sin(2*alpha)) ;
Sgw = 2*R*(pi-alpha) ;
Si = 2*R*sin(alpha) ;
Dg = 4*Ag/(Sgw+Si) ;
Al = (R.^2)*(alpha-0.5*sin(2*alpha)) ;
Slw = 2*R*alpha ;
Dl = 4*Al/Slw ;
ag = Ag/A ;
y=((X.^2).*(1-ag).^(n-2)*(D/Dl).^(n+1))-(ag.^(m-2)*(D/Dg).^(m+1)*(1+(Dg*Si/Al)));
end
WHERE
function [X] = setX
global X ;
X=linspace(0,100,101) ;
end
AND
function [Cg, Cl, n, m] = setglbl
global Cg Cl n m
n=1;
m=1;
end
AND
function [D, A, R] = geom()
D = 0.3; %Pipe Diameter (m)
R = D/2; %Pipe Radius (m)
A = pi*(R^2); %Cross-sectional Area (m^2)
end
I KEEP GETTING ERRORS:
Error using fzero (line 301)
FZERO cannot continue because user-supplied function_handle ==> equations failed with the error below.
*Matrix dimensions must agree.*
>> equations
*Not enough input arguments.*
Error in equations (line 7)
Ag = (R.^2)*(pi-alpha+0.5*sin(2*alpha)) ; % [m^2] Cross-sectional area of gas phase in pipe
Does anyone have some advice on what the problem could be?
Should the X interval be defined differently? Or should X be used as a second input to the function?
Any help appreciated!
采纳的回答
更多回答(2 个)
Torsten
2017-3-17
Does this work ?
function [alpha] = getalphach
[A] = geom();
[n, m, Z] = setglblch();
X = setX();
al = setal();
for i=1:numel(X)
Xloc = X(i);
for j = 1:numel(al)
alloc = al(j);
alpha(i,j) = fzero(@(x)equationsch(x,Xloc,A,m,n,Z,alloc),[1e-4, pi-1e-4]);
end
end
Best wishes
Torsten.
3 个评论
Try to call fzero with a single starting value, e.g.
alpha(i,j) = fzero(@(x)equationsch(x,Xloc,A,m,n,Z,alloc),pi-1e-4);
If fzero cannot find a solution, you should first plot your function to see whether a zero really exists.
By the way: Your call to fzero must be with a small x:
Not:
alpha(i,j) = fzero(@(x)equationsch(X,Xloc,m,n,Z,alloc),[1e-4, pi-1e-4]);
But:
alpha(i,j) = fzero(@(x)equationsch(x,Xloc,m,n,Z,alloc),[1e-4, pi-1e-4]);
Best wishes
Torsten.
Torsten
2017-3-22
Sure.
function [alpha] = getalphach
[A] = geom();
[n, m, Z] = setglblch();
[X,al] = setmartvar();
for i=1:numel(X)
Xloc = X(i);
alloc = al(i);
alpha(i) = fzero(@(x)equationsch(x,Xloc,A,m,n,Z,alloc),[1e-4, pi-1e-4]);
end
Best wishes
Torsten.
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!