HELP- MATLAB FREEZING/ NOT OUTPUTTING CODE

1 visualización (últimos 30 días)
Sarah Fullerton
Sarah Fullerton el 14 de Mzo. de 2019
Editada: Jan el 15 de Mzo. de 2019
Hi, matlab seems to be struggling to run my code, i cant see any obvious issues and it is not outputting anything just kind of freezing? can anyone spot what the issue may be? I have attached my code
clc
clear
close all
%%question C
Y=1.4; %heat capacity ratio
r=8:0.01:24;%compression ratio array V1/V2
re=4:0.01:20;%Expanison ratio array V4/V3
ro=10; %compression ratio of otto engine
etaotto=1-ro^(1-Y); %efficency of otto engine
eta=zeros(length(r),length(re)); %empty matrix
etaotto=zeros(length(r),length(re));%empty matrix
for i=1:length(r) %loop of all r values
for j=1:length(re)%loop of all re values
if r(i)>re(j)%checks if the r>re
eta(j,i)=1-((1/Y).*((re(j).^(-Y))-(r(i).^(-Y)))./((1./re(j))-(1./r(i))));
%Equation was derived in part A
end
if etaotto>eta(j,i)
%checks to see whether the diesel engine is less efficent than
%the otto engine with compression ratio
eta(j,i)=0; %sets diesel engine efficency to 0
end
end
end
imagesc(r,re,eta)%specifies image location
set(gca,'YDir','normal'); %Flips the y axis
title('Otto Engines V Diesel Engines')%title of the graph
xlabel('Compression ratios (V1/V2)')%x-axis label
ylabel('Expanison Ratios (V4/V3)')%y-axis label
x=colorbar;%shows the colour scale of the vertical colorbar
x.Label.String=('Efficency, \eta'); %label for colorbar
colormap winter %changes graph colour
  1 comentario
Luna
Luna el 14 de Mzo. de 2019
You have 2 loops and doing 1601*1601 = 2.563.201 iterations, so it would obviously takes long time.
What is the operation you need to do in a mathematical way? Can't you just do it by matrix operations instead of using for loops?

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 15 de Mzo. de 2019
Editada: Jan el 15 de Mzo. de 2019
It will increase your code, if you clean it up: Look at the orange marks in the editor, which show you, that etaotto have been defined twice. If you want the scalar value 1-ro^(1-Y) omit the redefinition as a matrix. Currently the comparison etaotto>eta(j,i) wastes a lot of time, because etaotto is a large matrix. With the scalar the code runs much faster.
By the way, there is a typo:
eta=zeros(length(r),length(re));
for i=1:length(r) %loop of all r values
for j=1:length(re)%loop of all re values
...
eta(j,i) = ...
You have defined eta with [length(r) x length(re)], but in the loop the indices are swapped. I guess, you want:
eta(i, j) = ...
A vectorization will help also:
Y = 1.4; %heat capacity ratio
r = 8:0.01:24;%compression ratio array V1/V2
re = 4:0.01:20;%Expanison ratio array V4/V3
ro = 10; %compression ratio of otto engine
etaotto = 1-ro^(1-Y); %efficency of otto engine
eta = zeros(length(re),length(r)); % Swapped dimensions
for i = 1:length(r) %loop of all r values
index = (r(i) > re);
eta(index, i) = 1-((1/Y).*((re(index).^(-Y))- ...
(r(i).^(-Y))) ./ ((1./re(index))-(1./r(i))));
end
This needs 0.2 seconds on my machine.
By the way, I'd reduce the number of parenthesis:
eta2(index, i) = 1 - (1 / Y .* (re(index).^(-Y) - r(i).^(-Y)) / Y ./ ...
(1 ./ re(index) - 1 ./ r(i)));
The comment is misleading:
eta=zeros(length(r),length(re)); %empty matrix
No, zeros are not "empty".

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by