Using fzero function to solve a nonlinear function with two inputs

4 vues (au cours des 30 derniers jours)
Egle
Egle le 16 Mar 2017
Commenté : Star Strider le 17 Mar 2017
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!

Réponse acceptée

Star Strider
Star Strider le 16 Mar 2017
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 commentaires
Star Strider
Star Strider le 16 Mar 2017
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 le 17 Mar 2017
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.

Connectez-vous pour commenter.

Plus de réponses (1)

Egle
Egle le 17 Mar 2017
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 commentaires
Egle
Egle le 17 Mar 2017
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 le 17 Mar 2017
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Numerical Integration and Differentiation dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by