Using fzero function to solve a nonlinear function with two inputs
4 次查看(过去 30 天)
显示 更早的评论
Hi,
My aim is to solve a non-linear equation for alpha with given values of X and al (both ranges of values). But I keep getting errors. Here is the code,
INPUTS:
function [n, m, Z] = setglblch
n=1 ;
m=1 ;
Z=14;
end
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
function [X] = setX
X=linspace(0,100,101) ;
end
function [al] = setal
al=linspace(0,1,101) ;
end
SOLVING:
function y = equationsch(alpha, X, A, m, n, Z, al)
beta1 = (1-al).^(-1) ;
beta2 = al/((1/A)-al) ;
beta3 = 1/alpha ;
beta4 = beta1-beta2.*beta3 ;
beta = beta4.^(-1) ;
if X == 0
y = 0;
else
y1 = al.^(1-0.5) ;
y2 = (1-al).^(0.5*m-1) ;
y3 = X/Z ;
y4 = beta.^(1+m) ;
y5 = alpha.^(1+n) ;
y6 = (y4/y5).*y3 ;
y = y1.*y2-y6 ;
end
end
WITH ONE INPUT X:
function [alpha] = getalphach
[A] = geom();
[n, m, Z] = setglblch();
X = setX();
al = setal();
alpha = arrayfun( @(x) fzero( @(alpha) equationsch(angle, X, D, A, R, m, n, Z, al), [1E-4, pi-1E-4]), X );
end
I GET ERROR Not enough input arguments.
AND WITH TWO INPUTS X AND al:
function [alpha] = getalphach
[A] = geom();
[n, m, Z] = setglblch();
X = setX();
al = setal();
alpha = arrayfun( @(x) fzero( @(alpha) equationsch(angle, X, D, A, R, m, n, Z, al), [1E-4, pi-1E-4]), X, al );
end
I GET ERROR Too many input arguments.
How can this be fixed?
Any help truly appreciated!
0 个评论
采纳的回答
Star Strider
2017-3-16
You need to change the argument from ‘alpha’ to ‘angle’ in the argument list for ‘equationch’.
With that change, it becomes:
alpha = arrayfun( @(x) fzero( @(angle) equationsch(angle, X, D, A, R, m, n, Z, al), [1E-4, pi-1E-4]), X, al );
I did not run your code, but that should resolve at least that problem.
3 个评论
Star Strider
2017-3-16
My pleasure!
Note that in your function definition:
function y = equationsch(alpha, X, A, m, n, Z, al)
‘equationsch’ has 7 arguments.
In this line:
alpha = arrayfun( @(x) fzero( @(angle) equationsch(angle, X, D, A, R, m, n, Z, al), [1E-4, pi-1E-4]), X, al );
you are passing it 9 arguments. That is likely what is throwing the error.
更多回答(1 个)
Egle
2017-3-17
4 个评论
Star Strider
2017-3-17
My pleasure.
It does help. The arrayfun function may not be able to do that.
I noticed that Torsten posted workable code that will do what you want (with a double loop) in your other related question. Torsten’s is probably the only workable solution.
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!