Using fzero function to solve a nonlinear function with two inputs

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!

 採用された回答

Star Strider
Star Strider 2017 年 3 月 16 日

0 投票

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 件のコメント

Egle
Egle 2017 年 3 月 16 日
Hello,
Well spotted, this is actually a typo, I am only using alpha argument in this model (angle was from a previous model).
However, after fixing that, the code still does not run and gives the same error.
Thanks!
Star Strider
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.
Egle
Egle 2017 年 3 月 17 日
Hi again,
Thank for your reply! I fixed them both to have 7 arguments, but it did not help. I keep getting error "Too many input arguments" if I try two inputs X and al.

サインインしてコメントする。

その他の回答 (1 件)

Egle
Egle 2017 年 3 月 17 日

0 投票

REVISED CODE:
INPUTS
function [n, m, Z] = setglblch
n=1 ;
m=1 ;
Z=14;
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, al, m, n, Z)
beta1 = alpha.*(1-al) ;
beta2 = alpha-al ;
beta = beta1./beta2 ;
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
function [alpha] = getalphach
[n, m, Z] = setglblch();
X = setX();
al = setal();
alpha = arrayfun( @(x) fzero( @(alpha) equationsch(alpha, X, al, m, n, Z), [1E-4, pi-1E-4]), X, al );
end
I GET ERROR:
>> getalphach
Error using getalphach>@(x)fzero(@(alpha)equationsch(alpha,X,al,m,n,Z),[1E-4,pi-1E-4])
Too many input arguments.

4 件のコメント

Star Strider
Star Strider 2017 年 3 月 17 日
The ‘x’ argument here:
@(x)fzero(@(alpha)equationsch(alpha,X,al,m,n,Z),[1E-4,pi-1E-4])
has to be present in the argument list for ‘equationsch’. It may need to be ‘alpha’ or ‘X’ instead.
(I am having a very difficult time understanding what you are doing, and have not yet succeeded.)
Torsten
Torsten 2017 年 3 月 17 日
編集済み: Torsten 2017 年 3 月 17 日
Egle wants to avoid a double loop and call "fzero" for all possible combinations of the array elements from X and al.
Best wishes
Torsten.
Egle
Egle 2017 年 3 月 17 日
Star Strider,
Thank you for the reply. I understand now that a "small x" should be used instead of capital X in the argument list for equationsch.
What I am trying to do: The equation y is a function of X, al and alpha. (y=0) I want to feed a range of values of X and a range of values of al to solve for alpha.
Does this help?
Egle
Star Strider
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.

サインインしてコメントする。

カテゴリ

製品

質問済み:

2017 年 3 月 16 日

コメント済み:

2017 年 3 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by