Error plotting fzero function - "Operands to the || and && operators must be convertible to logical scalar values"
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
DGM
le 7 Avr 2021
Modifié(e) : DGM
le 7 Avr 2021
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 commentaires
DGM
le 9 Avr 2021
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')
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Specifying Target for Graphics Output dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!