How to get fzero to work on an array?

29 ビュー (過去 30 日間)
Daniel Beeson
Daniel Beeson 2020 年 5 月 24 日
回答済み: Walter Roberson 2021 年 12 月 8 日
I'm trying to solve fzero for each value of x in an array, and I get theis error:
Operands to the || and && operators must be convertible to logical scalar values.
Error in fzero (line 444)
while fb ~= 0 && a ~= b
Error in hw5_code (line 14)
mach = fzero(eqn,[.16 2]);
x = .2:.1:.6;
r = .3 + 3.*((x-.5).^2);
g = 1.4;
p0 = 7576;
t0 = 1033;
pb = 100;
a = pi.*r.^2;
a_star = pi.*(.3^2);
eqn = @(m) (a./a_star).^2 - (1./m.^2 .*((2./(g+1)) .* (1 + (((g-1)./2) .* m.^2))).^((g+1)./(g-1)));
mach = fzero(eqn,[.16 2]);
t = t0 .* ((1+ (((g-1)/2) .* mach.^2)).^-1);
p = p0 .* (1+ (((g-1)/2) .* mach.^2)).^-(g/(g-1));
rho = p./(287.*t);
air = sqrt(g.*287.*t);
v = mach.*air;
m_dot = rho .* v .* a;
a1 = 1 + ((2.*g)./(1+g)).*((mach.^2) -1);
a2 = (2 + (g-1).*(mach.^2))./((g+1).*(mach.^2));
change_entropy = log(a1.*a2) - (((g-1)/g).*log(a1));
  3 件のコメント
Star Strider
Star Strider 2020 年 5 月 24 日
A (1x2) vector is entirely appropriate, providing a sign change exists on the interval.
Stephen23
Stephen23 2020 年 5 月 24 日
編集済み: Stephen23 2020 年 5 月 24 日
"Why are you passing a 1x2 vector as an initial guess to fzero()?"
The fzero documentation states that x0 may be scalar or a 2-element vector, in which case "fzero checks that fun(x0(1)) and fun(x0(2)) have opposite signs, and errors if they do not. It then iteratively shrinks the interval where fun changes sign to reach a solution." This is clearly very useful when the bracketing range is known.

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

回答 (2 件)

Alan Weiss
Alan Weiss 2020 年 5 月 26 日
I don't know if this is what you wanted, but I got it to work by making a loop for fzero:
x = .2:.1:.6;
r = .3 + 3.*((x-.5).^2);
g = 1.4;
p0 = 7576;
t0 = 1033;
pb = 100;
a = pi.*r.^2;
a_star = pi.*(.3^2);
eqn = @(m) (a./a_star).^2 - (1./m.^2 .*((2./(g+1)) .* (1 + (((g-1)./2) .* m.^2))).^((g+1)./(g-1)));
%%
% mach = fzero(eqn,[.16 2]);
mach = zeros(size(eqn(.16)));
for i = 1:length(mach)
tp = zeros(size(mach));
tp(i) = 1;
mach(i) = fzero(@(x)dot(tp,eqn(x)),[0.15 1]);
end
%%
t = t0 .* ((1+ (((g-1)/2) .* mach.^2)).^-1);
p = p0 .* (1+ (((g-1)/2) .* mach.^2)).^-(g/(g-1));
rho = p./(287.*t);
air = sqrt(g.*287.*t);
v = mach.*air;
m_dot = rho .* v .* a;
a1 = 1 + ((2.*g)./(1+g)).*((mach.^2) -1);
a2 = (2 + (g-1).*(mach.^2))./((g+1).*(mach.^2));
change_entropy = log(a1.*a2) - (((g-1)/g).*log(a1));
Alan Weiss
MATLAB mathematical toolbox documentation
  2 件のコメント
Nabilla Dewi Septiani
Nabilla Dewi Septiani 2021 年 12 月 8 日
Hello sir, I have similar problem with this, but in my case I have 2 variables that are in array. I have tried using the same code, but I got this error
Error using fzero (line 274)
Function values at the interval endpoints must differ in sign.
Error in Term_Paper (line 14)
x(i)=fzero(@(y)dot(tp,fx(x)),[0.15 1]);
Anyone can give a solution? Please
clear all; close all; clc;
%Parameters
xin=0.2; xout=0.1; Pr=0.3; qf=1;
y=0:0.1:1;
alpha=0:100:1000;
%For Cross-Flow Module (CF)
%y=(1+(x+Pr).*(alpha-1))-((1+(x+Pr).*(alpha-1)).^2-(4.*alpha.*(alpha-1).*x.*Pr)).^0.5/(2.*Pr.*(alpha-1))
%Calculating x
fx=@(x) ((1+((x+Pr).*(alpha-1))-((((1+((x+Pr).*(alpha-1))).^2)-(4.*alpha.*(alpha-1).*x.*Pr)).^0.5))/(2.*Pr.*(alpha-1)))-y;
x=zeros(size(fx(0.16)));
for i=1:length(x)
tp=zeros(size(x));
tp(i)=1;
x(i)=fzero(@(y)dot(tp,fx(x)),[0.15 1]);
end
%qfout-qf=-(1-Pr+((alpha-1).*(x-Pr.*y)))*a
%x-xin=(-(1/qf).*(alpha.*(x-(Pr.*y))*a)+(-x.*ln(qfout/qf))
syms a qfout
E=(-(1-Pr+((alpha-1).*(x-Pr.*y)))*a)-qfout+qf==0 , (-(1/qf).*alpha.*(x-(Pr.*y))*a)+(-x.*log(qfout/qf))-x+xin==0;
S=solve(E,a,qfout)
plot(double(alpha),double(E))
xlabel('Selectivity, alpha'); ylabel('Dimensionless Total Membrane Area, a_T');
Alan Weiss
Alan Weiss 2021 年 12 月 8 日
I suggest that you use the debugger and put a break point before the loop that contains the fzero call. Check to see whether the function values have different signs at the interval endpoints. Adjust your code so that they always do.
Alan Weiss
MATLAB mathematical toolbox documentation

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


Walter Roberson
Walter Roberson 2021 年 12 月 8 日
I'm trying to solve fzero for each value of x in an array, and I get theis error:
Don't Do That.
fzero() is defined as only accepting scalar equations of one variable. It cannot in itself be used to solve at multiple x values simultaneously
You need to loop through the different x values, constructing the other values such as a or r as you go.
If you make a and r into functions of the current x value, then you can probably arrayfun() the fzero() call.

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by