0% found this document useful (0 votes)
22 views6 pages

Matlab Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views6 pages

Matlab Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

12.

44 PROGRAMMING IN C& MA TLAB

MATLAB PROGRAMMING EXAMPLES


Program I
Write a MATLAB program to determine a given integer number is even orodd.
Solution:
8Script file:even__odd
$Program to determine whether a given number is even or odd
$Prompt the user to enter an integer number
num=input (' Enter a number: );
if (mod (num, 2) ==0)
fprintf ('%d is even\n', num);
else
fprintf(' d is odd \n', num) ;
end

Now exe cute the program


>>even_ oddJ
Enter a number: 2017.J
2017is odd
>>even_oddJ
Enter a number: 2018.J
2018 is even

Program 2
Write a program which reads a student marks in a test and then prints whether
he is passed or failed.
Solution:
We simply compare the marks with 35 in the if expression. If it is true we will
write disp( )to display "Passed" else "Failed".
The MATLAB program is shown below.
%Scriptfile: result .m
This program determines whether a student passed or failed
%Illustration of if/else
%Define variables
&mark --- student marks
%Pronpt the user for marks of a student
mark=input ( Enter student marks:);
i f marks>=35
disp('Passed');
else
disp(Failed') ;
end

(A.P) (RADIANCE PuRLISHERS)


CHAPTER-12
:,B A SICS OF MATILAR 12.45
Program3
Write aM¤TLAB program to find the largest of three numbers.
Solution:
ascriptfile :maximum. m
eprogr am tO find the largest of three numbers
8Prompt the user to give input data
a=input(' Enter First Number : );
b=input ('Enter Second Number : );
c=input (' Enter Third Number: ');
&Tllustration of if/elseif/else structure
if (a>b) &&(a>c)
fprintf ('The Largest number is %7.2f\n',a) ;
elseif (b>c) &&(b>a)
fprintf ('The Largest number is %7.2f\n',b);
else
%7.2f\n',c) ;
fprintf ('The Largest number is
end

Now execute the above program


>>maximum. mJ
Enter First Number : 78.12.J
45.78.J
Enter Second Number :
Enter Third Number 90.J
90.00
The largest number is
Progranm I
Write a MATLAB program to calculate the sum of squares of all integers between
land 10:
Solution:
8Scriptfile:sum_squares.m
$Program to calculate sum of squares of all
8Integers between 1 and 10
$Illustration of while loop
sum=0;
n=1;
while n<=10
Sum=sum+n^2 ;
n=n+1;
end
fprintf ('1^2+2 ^2+3 ^2+.. .+10^2 =%d\n',sum) ;
% (0r) disp (sum) ;
Now save and execute the program by pressing F5.
>>Sum_squaresJ
1^2+2^2+3^2 +. ..+10^2=385
Program 2
12.55
CHAPTER-12 ::*** BAsTCS OF MA TLAR
MATLAB PROGRAMMING EXAMPLES
Program 1
'n' natural numbers using tor
Write a simple MATLAB program to generate first
loop.
Solution:
using for loop
8Program to Generate Natural numbers
n=input (' Enter an integer:');
disp(The natural numbers are:');
for i=l :n
fprintf('$3d\n',i) ;
end

Program 2
n is the product of consecutive integers from 1to n.
The factorialof an integer
That is,
factorial n=n!=n x (n-1) x (n-2) X * X1
the factorial for any given n.
Write a MATLAB program to compute
Solution:
%Scriptfile:n_factorial
factorial n
8Programe to compute
integer: ') ;
n=input ('Enter positive
factorial=1; %initialize
8Illustration of for loop
%go from 1 to n
for k=1 :n
factorial =factorial*k; &multiply 1 by 2, 3, 4, etc.

end
factorial) ;
fprintf ('%3d! =%d\n',n,
the file. For this simply
Nowexecute the above program to saving and naming
press F5 function key.
>>n factorialJ
Enter positive integer : 12
12!=479001600

>>n factorialJ

Enter positive integer : 8J


8!=40320

(RADIANCE PUBLISHERS)
Program 1
Write a script file to plotthe function y= x for the range of values for x from 0
to 100, with an increment of 5.
Solution :
%Program to plot the curve y=x
x=0:5:100 create vector x
y=x; %calculate y
plot (x, y) %plot x vs .y

When you run the file, MATLAB displays the following plot.
100
90
80
70

60

50

40

30

20

10
0 10 20 30 40 50 60 70 80 90 100

Fig. 12.10

Program 2
AWrite a script file to plot the curve y=x*, - 100 < x< 100.
Solution :
%Program to plot the curve y=x^2
X=-100:20: 100;

y=x. ^2 ;

plot (x, y)

(A.P) (RADIANCE PURISHERSy


CHAPTER-12 :**BASICS OF MATLAB 12.63

When you run the file, MATLAB displays the following plot.
10000

9000

8000

7000 -

6000

5000

4000

3000

2000

1000

0 20 40 60 80 100
-100 -80 -60 40 -20

Fig. 12.11

You might also like