0
Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Matlab 4

230

Published on

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
230
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
2
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. MATLAB Lecture-4
• 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. 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. If Statement
• Simple if Statement
• if logical expression1
• statement group1
• end
• Nested if statement
• if logical expression1
• statement group1
• if logical expression2
• statement group2
• end
• statement group3
• end
• statement group4
• 5. If else statements
• if else statement
• if logical expression1
• statement group1
• else
• statement group2
• end
• if elseif statement
• if logical expression1
• statement group1
• elseif logical expression2
• statement group2
• end
• 6. If elseif else statement
• if logical exression1
• statement group1
• elseif logical expression2
• statement group2
• elseif logical expression3
• statement group3
• elseif logical expression4
• statement group4
• else
• statement group5
• end
• 7. Switch-case statement
• switch switch expression
• case value1
• … ..
• … ..{ Group 1 of Commands}
• case value1
• … ..
• … ..{ Group 2 of Commands}
• otherwise
• … ..
• … ..{ Group 3 of Commands}
• end
• 8. Assignment-1
• 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.
• y=0 x<-2.5
• =1 -2.5 < x<-1.5
• =2 -1.5 < x<-0.5
• =3 -0.5 < x<0.5
• =4 0.5 < x<1.5
• =5 1.5 < x<2.5
• =6 2.5 < x<3.5
• =7 x > 3.5
• Test for condition with amplitudes -1.25, 2.57 and 6.
• 9. Solution using if else
• function y_dig=bitatd_(x-analog)
• if x-analog<-2.5
• y_dig=0;
• elseif x-analog>=-2.5 & x-analog<-1.5
• y_dig=2;
• …… .(so on) elseif x-analog>=2.5
• y_dig=6;
• else y_dig=7;
• end
• 10. For loops
• It allows a group of statements to be repeated a fixed number of times.
• for index = expression
• statement group X
• end
• expression may be of the form m:n or m:i:n
• where m is beginning value, n is ending value and i is increment.
• 11. Assignment-2
• Find out the squares of integers from 1 to 100 and show their sum.
• Fill a 10 by 20 matrix with unity to demonstrate the use of nested ‘for’ operations
• 12. Solution
• sum=0;
• for i=1:100
• sum=sum+i^2;
• end
• sum
• n=10;
• m=20;
• for i=1:n
• for j=1:m
• b(i,j)=1;
• end
• end
• b
• 13. While Loops
• A while loop allows one to repeat a group of statements as long as the specified condition is satisfied.
• while expression1
• statement group1
• end
• statement group2
• 14. Assignment-3
• Determine the number of consecutive integers that when added together will give a value equal or just less than 210.
• 15. Solution
• int=1;
• int_sum=0;
• max_val=210;
• while int_sum<max_val
• int_sum=int_sum+int;
• int=int+1;
• end
• last_int=int
• if int_sum==max_val
• num_int=int-1
• tt_int_ct=int_sum
• elseif int_sum>max_val
• num_int=int-1
• tt_int_ct=int_sum-last_int
• end
• 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. Assignment-4
• Using disp Command generate the following matrix:-
• 1 0 0
• 0 1 0
• 0 0 1
• The above matrix should be generated using (a) Iterative method (b) Basic MATALAB command in one line.
• 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.
• 18. Solution 1
• a.) clc; clear all; close all;
• for i=1:3
• for j=1:3
• if i==j
• a(i,j)=1;
• else
• a(i,j)=0;
• end
• end
• end
• b.) disp(eye(3,3))
• 19. Solution 2
• clc; clear all; close all;