A problem with While command...

2 views (last 30 days)
Giacomo
Giacomo on 17 Jan 2013
Hi everyone,
I have a problem using While command so Matlab becomes busy.
This is a part of code (the variables are all defined):
a=zeros(length(r),1);
anew=1./(((4.*((sind(fi)).^2)./(sigma.*Cn)))+1);
err=(anew-a)./anew;
toll=1e-3;
while abs(err)>=toll a=anew; end
If I break the loop pressing Ctrl+C and I ask "a" value from command window it returns me the "anew" value calculated in first step of loop. If i calculate by myself the other values (second, third ecc. step)the condition of while loop is still true. There is a syntax error or is only the function that doesn't converge?
Thanks for any help and sorry for my english!

Answers (6)

Jan
Jan on 17 Jan 2013
Edited: Jan on 17 Jan 2013
But in the body of the loops the values of teh variables do not change at all. All that happens is, that the value of anew is assigned to a again and again. and anew is not calculated in the first step of the loop, but only once before the loop.
It seems like you have confused the variable anew with a function, and the same for err, which does not change its value inside the loop also.

Thorsten
Thorsten on 17 Jan 2013
Based on Jan's comments you may want to change your code as sketched below:
a=zeros(length(r),1);
anew=1./(((4.*((sind(fi)).^2)./(sigma.*Cn)))+1);
err=(anew-a)./anew;
toll=1e-3;
while abs(err)>=toll
a=anew;
Change values in the loop such that anew gets a new value in every iteration
fi =
Cn =
anew=1./(((4.*((sind(fi)).^2)./(sigma.*Cn)))+1);
Compute new error WITHIN loop
err=(anew-a)./anew;
end
  1 Comment
Jan
Jan on 17 Jan 2013
In addition err is a vector. What should happen in while abs(err)>=toll exactly? If all elements >= 0, or any elements?
I assume, err should be a scalar.

Sign in to comment.


Giacomo
Giacomo on 17 Jan 2013
Thanks for helping me. I just started using Matlab. So, this is the entire code:
close all
clear all
clc
%Carico dati geometrici pala: r - beta - chord
load geom.txt
rpm=27.1;
omega=rpm*2*pi/60; %velocità del rotore in rad/s
r=geom(:,1);
beta=geom(:,2);
chord=geom(:,3);
tetap=0;
teta=tetap+beta;
ro=1.225; %densità dell'aria
mu=1.8*10^(-5); %viscosità cinematica dell'aria a 20°C
B=3; %numero delle pale
%calcolo delle velocità
V=4;
a=zeros(length(r),1);
a1=zeros(length(r),1);
%calcolo della solidità
sigma = zeros(length(r),1);
for i = 1:length(r)
sigma(i)=chord(i).*B./(2*pi*r(i));
end
%calcolo di fi
fi = zeros(length(r),1);
for i = 1:length(r)
fi(i)=(atan((((1-a(i)).*V)/((1+a1(i)).*omega.*r(i))))).*180./pi;
end
%calcolo angolo di attacco alfa
alpha = zeros(length(r),1);
for i=1:length(r)
alpha(i)=fi(i)-teta(i);
end
%calcolo numero di Reynolds
Re = zeros(length(r),1);
for i = 1:length(chord)
Re(i)=((ro/mu)*V)*chord(i);
end
%calcolo Cl e Cd
cl=zeros(length(r),1);
for i=1:length(fi)
cl(i)=-0.0002.*(alpha(i).^3)-0.0008.*(alpha(i).^2)+0.1152.*alpha(i)+0.3502;
end
cd=zeros(length(r),1);
for i=1:length(fi)
cd(i)=0.0002.*(alpha(i).^2)+0.0001.*alpha(i)+0.005;
end
%calcolo Cn e Ct
for i=1:length(fi)
Cn=cl(i).*cosd(fi)+cd(i).*sind(fi);
end
for i=1:length(fi)
Ct=cl(i).*sind(fi)-cd(i).*cosd(fi);
end
%calcolo nuovi a e a'
anew=zeros(length(r),1);
for i=1:length(r)
anew(i)=1./(((4.*((sind(fi(i))).^2)./(sigma(i).*Cn(i))))+1);
end
a1new=zeros(length(r),1);
for i=1:length(r)
a1new(i)=1./(((4.*(sind(fi(i)).*cosd(fi(i)))./(sigma(i).*Ct(i))))-1);
end
%Calcolo errore relativo a ed a'
err=(anew-a)./anew;
err1=(a1new-a)./a1new;
toll=1e-3;
%Istruzioni ciclo while
while abs(err1)>=toll||abs(err)>=toll
a1=a1new;
a=anew;
end
If I understand correctly I have to put all the variables that change in the body of while loop. But for the err vector how can I resolve?
  1 Comment
Jan
Jan on 18 Jan 2013
Edited: Jan on 18 Jan 2013
When you have just started to program Matlab, where does the "close all, clear all, clc" come form? This appears very frequently, but is inspite of this not useful. There is simply no reasons to close all figures and "clear all" removes beside other things all loaded functions from the memory also. While a pure "clear" would remove all variables only, which could have an advantage, "clear all" is useful only, if you have modified all M-files during the program runs - a really rare situation. Therefore I'm interested to know, who did suggest the brute clearing method.

Sign in to comment.


Thorsten
Thorsten on 18 Jan 2013
Exactly, you have to put all values that change in the body of the while loop. And all values that do not change can be moved outside the body of the while loop to save computation time and to make the code clearer. And you can go on if the minimum of all values in err is above your tolerance. Use if you are happy if either a or a1 is below toll; use && if both have to be below tolerance
while min(abs(errl)) >= toll && min(abs(err)) > = toll
And make sure that you do not confuse a and a1. For example, the 6. line from below should read (replace a with a1):
err1=(a1new-a1)./a1new;

Jan
Jan on 18 Jan 2013
Matlab code looks much nicer, when you operate on vectors, matrices and arrays directly. Instead of:
alpha = zeros(length(r),1);
for i=1:length(r)
alpha(i)=fi(i)-teta(i);
end
write:
alpha = fi - teta;

Giacomo
Giacomo on 18 Jan 2013
I modified the code as you suggested and seems to work or at least it doesn't crash. Thank you so much.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!