Matlab 4

562 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
562
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Matlab 4

  1. 1. MATLAB Lecture-4
  2. 2. Relational Operators Ex- a=[1 2 3]; b=[1 2 2]; a==b returns ans=1 1 0 since the last case did not match. Relational Operator Meaning < Less than <= Less than or equal > Greater than >= Greater than or equal == Equal ~= Not equal
  3. 3. Logical Operators and Functions Logical Operator Meaning & And | Or ~ Not Function Name Description ischar(x) Returns a 1 if x is a character string & 0 otherwise isfinite(x) Returns a 1 if elements of x are finite else 0 isinf(x) Returns 1 where elements of x are infinite else 0 ismember(x,y) Returns 1 when elements of x are also in y else 0 isreal(x) Returns 1 whenever the elements of x have no imaginary part and a 0 otherwise
  4. 4. If Statement <ul><li>Simple if Statement </li></ul><ul><li>if logical expression1 </li></ul><ul><li>statement group1 </li></ul><ul><li>end </li></ul><ul><li>Nested if statement </li></ul><ul><li>if logical expression1 </li></ul><ul><li>statement group1 </li></ul><ul><li>if logical expression2 </li></ul><ul><li>statement group2 </li></ul><ul><li>end </li></ul><ul><li>statement group3 </li></ul><ul><li>end </li></ul><ul><li>statement group4 </li></ul>
  5. 5. If else statements <ul><li>if else statement </li></ul><ul><li>if logical expression1 </li></ul><ul><li>statement group1 </li></ul><ul><li>else </li></ul><ul><li>statement group2 </li></ul><ul><li>end </li></ul><ul><li>if elseif statement </li></ul><ul><li>if logical expression1 </li></ul><ul><li>statement group1 </li></ul><ul><li>elseif logical expression2 </li></ul><ul><li>statement group2 </li></ul><ul><li>end </li></ul>
  6. 6. If elseif else statement <ul><li>if logical exression1 </li></ul><ul><li>statement group1 </li></ul><ul><li>elseif logical expression2 </li></ul><ul><li>statement group2 </li></ul><ul><li>elseif logical expression3 </li></ul><ul><li>statement group3 </li></ul><ul><li>elseif logical expression4 </li></ul><ul><li>statement group4 </li></ul><ul><li>else </li></ul><ul><li>statement group5 </li></ul><ul><li>end </li></ul>
  7. 7. Switch-case statement <ul><li>switch switch expression </li></ul><ul><li>case value1 </li></ul><ul><li>… .. </li></ul><ul><li>… ..{ Group 1 of Commands} </li></ul><ul><li>case value1 </li></ul><ul><li>… .. </li></ul><ul><li>… ..{ Group 2 of Commands} </li></ul><ul><li>otherwise </li></ul><ul><li>… .. </li></ul><ul><li>… ..{ Group 3 of Commands} </li></ul><ul><li>end </li></ul>
  8. 8. Assignment-1 <ul><li>Using a MATALAB function file write a code for A/D converter (if else) where ‘x’ is i/p and ‘y’ is digital o/p governed by following eq. </li></ul><ul><li>y=0 x<-2.5 </li></ul><ul><li> =1 -2.5 < x<-1.5 </li></ul><ul><li>=2 -1.5 < x<-0.5 </li></ul><ul><li>=3 -0.5 < x<0.5 </li></ul><ul><li>=4 0.5 < x<1.5 </li></ul><ul><li>=5 1.5 < x<2.5 </li></ul><ul><li>=6 2.5 < x<3.5 </li></ul><ul><li>=7 x > 3.5 </li></ul><ul><li>Test for condition with amplitudes -1.25, 2.57 and 6. </li></ul>
  9. 9. Solution using if else <ul><li>function y_dig=bitatd_(x-analog) </li></ul><ul><li>if x-analog<-2.5 </li></ul><ul><li>y_dig=0; </li></ul><ul><li>elseif x-analog>=-2.5 & x-analog<-1.5 </li></ul><ul><li>y_dig=2; </li></ul><ul><li>…… .(so on) elseif x-analog>=2.5 </li></ul><ul><li>y_dig=6; </li></ul><ul><li>else y_dig=7; </li></ul><ul><li>end </li></ul>
  10. 10. For loops <ul><li>It allows a group of statements to be repeated a fixed number of times. </li></ul><ul><li>for index = expression </li></ul><ul><li>statement group X </li></ul><ul><li>end </li></ul><ul><li>expression may be of the form m:n or m:i:n </li></ul><ul><li>where m is beginning value, n is ending value and i is increment. </li></ul>
  11. 11. Assignment-2 <ul><li>Find out the squares of integers from 1 to 100 and show their sum. </li></ul><ul><li>Fill a 10 by 20 matrix with unity to demonstrate the use of nested ‘for’ operations </li></ul>
  12. 12. Solution <ul><li>sum=0; </li></ul><ul><li>for i=1:100 </li></ul><ul><li>sum=sum+i^2; </li></ul><ul><li>end </li></ul><ul><li>sum </li></ul><ul><li>n=10; </li></ul><ul><li>m=20; </li></ul><ul><li>for i=1:n </li></ul><ul><li>for j=1:m </li></ul><ul><li>b(i,j)=1; </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>b </li></ul>
  13. 13. While Loops <ul><li>A while loop allows one to repeat a group of statements as long as the specified condition is satisfied. </li></ul><ul><li>while expression1 </li></ul><ul><li>statement group1 </li></ul><ul><li>end </li></ul><ul><li>statement group2 </li></ul>
  14. 14. Assignment-3 <ul><li>Determine the number of consecutive integers that when added together will give a value equal or just less than 210. </li></ul>
  15. 15. Solution <ul><li>int=1; </li></ul><ul><li>int_sum=0; </li></ul><ul><li>max_val=210; </li></ul><ul><li>while int_sum<max_val </li></ul><ul><li>int_sum=int_sum+int; </li></ul><ul><li>int=int+1; </li></ul><ul><li>end </li></ul><ul><li>last_int=int </li></ul><ul><li>if int_sum==max_val </li></ul><ul><li>num_int=int-1 </li></ul><ul><li>tt_int_ct=int_sum </li></ul><ul><li>elseif int_sum>max_val </li></ul><ul><li>num_int=int-1 </li></ul><ul><li>tt_int_ct=int_sum-last_int </li></ul><ul><li>end </li></ul>
  16. 16. Some basic I/O commands Command Description break Exits while or for loop disp Displays text or matrix echo Displays m-files during execution error Displays error messages format Switches output display to a particular format fprintf Displays text and matrices and specifies format for printing values input Allows user input keyboard Invokes the keyboard as an m-file pause Causes an m-file to stop executing, pressing any key causes resumption of program execution
  17. 17. Assignment-4 <ul><li>Using disp Command generate the following matrix:- </li></ul><ul><li>1 0 0 </li></ul><ul><li>0 1 0 </li></ul><ul><li>0 0 1 </li></ul><ul><li>The above matrix should be generated using (a) Iterative method (b) Basic MATALAB command in one line. </li></ul><ul><li>2. Write a User friendly MATLAB program which should enter the age of the user and display an error message if age<0 and ask the user again to enter the age. </li></ul>
  18. 18. Solution 1 <ul><li>a.) clc; clear all; close all; </li></ul><ul><li>for i=1:3 </li></ul><ul><li>for j=1:3 </li></ul><ul><li>if i==j </li></ul><ul><li>a(i,j)=1; </li></ul><ul><li>else </li></ul><ul><li>a(i,j)=0; </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>b.) disp(eye(3,3)) </li></ul>
  19. 19. Solution 2 <ul><li>clc; clear all; close all; </li></ul><ul><li>x=input(‘Enter your age’); </li></ul><ul><li>if x<0 </li></ul><ul><li>error(‘Wrong age entered’); </li></ul><ul><li>end </li></ul><ul><li>x=input(‘Enter your age’); </li></ul>

×