Using fzero function to solve a nonlinear function with two inputs
Show older comments
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!
Accepted Answer
More Answers (1)
Egle
on 17 Mar 2017
4 Comments
Star Strider
on 17 Mar 2017
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.)
Egle
on 17 Mar 2017
Star Strider
on 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.
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!