Matlab Tutorial 7: Common Programming Structures and Conditional Statements


Timing

In some cases there could be limitations of time. How long a certain calculation are supposed to continue and therefore Matlab provides us with a tool to find out what parts of a program that are time-consuming. This is accomplished by the command profile.
Let us have a look on our previously used m-file nonsens. Write!

for variable=expression
control statements
end

Logical operators
& Logical and
| Logical or
~ Logical not
Finally we will look on three examples to see how we can use these operators.

i=1;
the_sum=0;
for i=1:10
the_sum=the_sum+1/(i^2);
end
fprintf('The sum of 10 elements gives: %f ', the_sum)
if x==3&k<100 % if x=3 and k<100, then display ”Hello !”
disp('Hello !')
else
disp('Hi !') % otherwise display ”Hi !”
end
% Logical OR
x=input('Give a x: ')
k=input('Give a k: ')
if x==3|k<100 % if x=3 or k<100, then display ”Hello !”
disp('Hello !')
else
disp('Hi !') % otherwise display ”Hi !”
end
% Logical NOT
k=input('Give a k: ')
if ~(k<100) % if k>=100, then display ”Hello !”
disp('Hello !')
else % otherwise display ”Hi !”
disp('Hi !')
end

Pages: 1 2 3 4

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment