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

조회 수: 1 (최근 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
Saumya Nagar 17BME0447 2021년 4월 9일
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에서 Optimization에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by