Error plotting fzero function - "Operands to the || and && operators must be convertible to logical scalar values"

2 次查看(过去 30 天)
Good Day!
I am trying to plot the solution of an equation using fzero function but getting the error - "Operands to the || and && operators must be convertible to logical scalar values". Can anyone please help me out.
The code is
clc;
clear all;
format short;
Throat_Diameter=2.8;
Exit_Diameter=5.76;
G=1.4;
Throat_Area=3.14159265.*(10.^(-6)).*((Throat_Diameter).^2)/4;
Exit_Area_inMeter=3.14159265.*(10.^(-6)).*((Exit_Diameter).^2)/4;
Diverging_Length= input('Enter the length of diverging section in mm: )' );
Distance_from_throat=linspace(1,Diverging_Length,50);
Section_Diameter=Throat_Diameter+((Exit_Diameter-Throat_Diameter)*Distance_from_throat)/Diverging_Length;
Section_Area=3.14159265.*(10.^(-6)).*((Section_Diameter).^2)/4;
ARatio=Section_Area/Throat_Area;
problem.objective = @(M) (1/M.^2).*(((2+(G-1).*M.^2)/(G+1)).^((G+1)/(G-1)))-ARatio.^2; % Objective function
problem.solver = 'fzero'; % Find the zero
problem.options = optimset(@fzero); % Default options
% Solve supersonic root
problem.x0 = [1+1e-6 50]; % Supersonic solver bounds
M_Local = fzero(problem); % Solve for supersonic M
plot(Distance_from_throat,M_Local)
The error is:
Operands to the || and && operators must be convertible to logical scalar values.
Error in fzero (line 421)
while fb ~= 0 && a ~= b
Error in PlottingAreaMachRelation (line 23)
M_Local = fzero(problem); % Solve for supersonic M

采纳的回答

DGM
DGM 2021-4-7
编辑:DGM 2021-4-7
You've set Distance_from_throat to a vector. As far as I know, fzero() handles scalars only. You can always find all the solutions with a simple loop.
clc; clf
clear all;
format short;
Throat_Diameter=2.8;
Exit_Diameter=5.76;
G=1.4;
Throat_Area=3.14159265.*(10.^(-6)).*((Throat_Diameter).^2)/4;
Exit_Area_inMeter=3.14159265.*(10.^(-6)).*((Exit_Diameter).^2)/4;
Diverging_Length= 5;%input('Enter the length of diverging section in mm: )' );
DFT=linspace(1,Diverging_Length,50);
M_Local=zeros([1 numel(DFT)]);
for d=1:numel(DFT)
Distance_from_throat=DFT(d);
Section_Diameter=Throat_Diameter+((Exit_Diameter-Throat_Diameter)*Distance_from_throat)/Diverging_Length;
Section_Area=3.14159265.*(10.^(-6)).*((Section_Diameter).^2)/4;
ARatio=Section_Area/Throat_Area;
problem.objective = @(M) (1/M.^2).*(((2+(G-1).*M.^2)/(G+1)).^((G+1)/(G-1)))-ARatio.^2; % Objective function
problem.solver = 'fzero'; % Find the zero
problem.options = optimset(@fzero); % Default options
% Solve supersonic root
problem.x0 = [1+1e-6 50]; % Supersonic solver bounds
M_Local(d) = fzero(problem); % Solve for supersonic M
end
plot(DFT,M_Local)
  2 个评论
Saumya Nagar 17BME0447
Thanks a lot for the answer. It worked!
I have another question, here I now need to put three different numerical values of G and plot the result on a single plot. Is there an easy way to do it by putting a loop, if yes, can you please let me know how should I write it. Thanks in advance!
DGM
DGM 2021-4-9
The simple way would be to add another loop. If we arrange the y-series as rows in the output matrix, we can plot them all at once.
Throat_Diameter=2.8;
Exit_Diameter=5.76;
G=[1.2 1.4 1.6];
Throat_Area=3.14159265.*(10.^(-6)).*((Throat_Diameter).^2)/4;
Exit_Area_inMeter=3.14159265.*(10.^(-6)).*((Exit_Diameter).^2)/4;
Diverging_Length= 5;%input('Enter the length of diverging section in mm: )' );
DFT=linspace(1,Diverging_Length,10);
M_Local=zeros([numel(G) numel(DFT)]);
for g=1:numel(G)
for d=1:numel(DFT)
Distance_from_throat=DFT(d);
Section_Diameter=Throat_Diameter+((Exit_Diameter-Throat_Diameter)*Distance_from_throat)/Diverging_Length;
Section_Area=3.14159265.*(10.^(-6)).*((Section_Diameter).^2)/4;
ARatio=Section_Area/Throat_Area;
problem.objective = @(M) (1/M.^2).*(((2+(G(g)-1).*M.^2)/(G(g)+1)).^((G(g)+1)/(G(g)-1)))-ARatio.^2; % Objective function
problem.solver = 'fzero'; % Find the zero
problem.options = optimset(@fzero); % Default options
% Solve supersonic root
problem.x0 = [1+1e-6 50]; % Supersonic solver bounds
M_Local(g,d) = fzero(problem); % Solve for supersonic M
end
end
h=plot(DFT,M_Local); grid on
aa=num2cell(G);
tagfun=@(x) sprintf('G = %2.2f',x);
legend(h,cellfun(tagfun,aa,'UniformOutput',false),'location','northwest')

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

产品


版本

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by