Programming

1,045 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,045
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
26
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Programming

  1. 1. Workshop on Basic Matlab Paresh Kamble
  2. 2. Types of Flow Control  Conditional Control – if, else, switch  Loop Control – for, while, continue, break  Error Control – try, catch  Program Termination – return
  3. 3. Conditional Control – if, else, switch  if, else and elseif: The if statement evaluates a logical expression and executes a group of statements when the expression is true. The optional elseif and else keywords provide for the execution of alternate groups of statements. An end keyword, which matches the if, terminates the last group of statements.
  4. 4. Conditional Control – if, else, switch  if, else and elseif: The MATLAB algorithm for generating a magic square of order n involves 3 different cases: when n is odd, when n is even but not divisible by 4, or when n is divisible by 4. This is described by: if rem(n,2) ~= 0 M = odd_magic(n) elseif rem(n,4) ~= 0 M = single_even_magic(n) else M = double_even_magic(n) end
  5. 5. Conditional Control – if, else, switch  if, else and elseif: To check for equality between two variables we use, It tests where they are equal A = magic(4); B = A; B(1,1) = 0; A == B ans = 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Isequal checks if they are equal isequal(A, B) ans = 0
  6. 6. Conditional Control – if, else, switch  if, else and elseif: Usually this situation will never go to else state: if A>B 'greater' elseif A < B 'less' elseif A == B 'equal' else error('Unexpected situation') end
  7. 7. Conditional Control – if, else, switch  switch and case: The logic of the magic squares algorithm can also be described by: switch (rem(n,4)==0) + (rem(n,2)==0) case 0 M = odd_magic(n) case 1 M = single_even_magic(n) case 2 M = double_even_magic(n) otherwise error('This is impossible') end
  8. 8. Loop Control – for, while  for: for n = 3:32 r(n) = rank(magic(n)); end for i = 1:m for j = 1:n H(i,j) = 1/(i+j); end end
  9. 9. Loop Control – for, while  while: a = 0; fa = -Inf; b = 3; fb = Inf; while b-a > eps*b x = (a+b)/2; fx = x^3-2*x-5; if sign( fx) == sign( fa) a = x; fa = fx; else b = x; fb = fx; end end
  10. 10. Thank you!

×