How to get fzero with 2 variables in array?

4 次查看(过去 30 天)
I want to get the x value for each y and alpha in array. But I got error like this
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]);
How to solve this?
%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

采纳的回答

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 alpha and y values. Your code would produce one output for each alpha and each y, so you need to output a 2D grid -- unless, that is, you intend each of those alpha values to be matched with a corresponding y value (as you have 11 of each): in that case you would only need a a vector output for each fzero system
arrayfun(@(ALPHA, Y) fzero(@(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), alpha, y)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Scope Variables and Generate Names 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by