You are on page 1of 19

Matlab 2nd sem

Dept Electronics & Communication S.E.T.Polytechnic Melkote

Write program for the following problems (Assume right units. Search and
use library functions wherever possible). Unless specified, built-in functions
may be used if necessary.
1. Display your country name.
2. Compute the area and circumference of a circle given the radius.
3. Compute simple interest given the interest rate, principal and
duration.
4. Compute compound interest given the interest rate, principal,
compounding-nature and duration.
5. Swap contents of two variables without using intermediate variables.
6. Factorial of a single digit number.
7. Absolute value of a number.
8. Largest of three numbers.
9. Logarithm of a number.
10.Y=sin(1+ 2)+cos(1- 2) given 1 and 2 in degrees.
11.Average of the numbers in 3x4 matrix.

Prepared By

Dayananda Gowda K.R Lecturer E&C Department

Page

Matlab 2nd sem

Dept Electronics & Communication S.E.T.Polytechnic Melkote

1. Display your country name.


clc;
close all;
clear all;
countryname=input('INDIA');
disp('countryname=');
disp(countryname);

Prepared By

Dayananda Gowda K.R Lecturer E&C Department

Page

Matlab 2nd sem

Dept Electronics & Communication S.E.T.Polytechnic Melkote

2. Compute the area and circumference of a circle given the radius.


clc;
close all;
clear all;
r=input('radius=');
disp('r=');
disp(r);
A=(3.14*r*r);
disp('area=');
disp(A);
C=(2*3.14*r);
disp('circumference=');
disp(C);

Prepared By

Dayananda Gowda K.R Lecturer E&C Department

Page

Matlab 2nd sem

Dept Electronics & Communication S.E.T.Polytechnic Melkote

3. Compute simple interest given the interest rate, principal and duration.
clc;
clear all;
close all;
p=input('principle amount p=');
r=input('rate of intrest b=');
t=input('time taken to return amount t=')
disp('principle amount p');
disp(p);
disp('rate of intrest r');
disp(r);
disp('time taken to return amount t');
disp(t);
I=p*r*t;
disp('intrest');
disp(I);

Prepared By

Dayananda Gowda K.R Lecturer E&C Department

Page

Matlab 2nd sem

Dept Electronics & Communication S.E.T.Polytechnic Melkote

Compute compound interest given the interest rate, principal, compoundingnature and duration.

clc;
close all;
clear all;
p=7500;
r=0.06;
n=12;
t=5;
A=p*(1+(r/n))^(n*t);
disp(A);

Prepared By

Dayananda Gowda K.R Lecturer E&C Department

Page

Matlab 2nd sem

Dept Electronics & Communication S.E.T.Polytechnic Melkote

1. Swap contents of two variables without using intermediate variables.


clc;
close all;
clear all;
x=7;
y=9;
[y,x]=deal(x,y);

Prepared By

Dayananda Gowda K.R Lecturer E&C Department

Page

Matlab 2nd sem

Dept Electronics & Communication S.E.T.Polytechnic Melkote

1. Factorial of a single digit number.

clc;
close all;
clear all;
f=factorial(22);
disp('f=');
disp(f);

Prepared By

Dayananda Gowda K.R Lecturer E&C Department

Page

Matlab 2nd sem

Dept Electronics & Communication S.E.T.Polytechnic Melkote

1. Largest of three numbers.


clc;
close all;
clear all;
A=[23,42,37,18];
M=max(A);
disp('maximum value=')
disp(M);

Prepared By

Dayananda Gowda K.R Lecturer E&C Department

Page

Matlab 2nd sem

Dept Electronics & Communication S.E.T.Polytechnic Melkote

Y=sin(1+ 2)+cos(1- 2) given 1 and 2 in degrees.


clc;
close all;
clear all;
y=sin(90+90)+cos(180+90);
disp('y=');
disp(y);

Prepared By

Dayananda Gowda K.R Lecturer E&C Department

Page

Matlab 2nd sem

Dept Electronics & Communication S.E.T.Polytechnic Melkote

Addition, of two matrices.

clc;
clear all;
close all;
a=input('matrix a=');
disp('matrix a')
disp(a);
b=input('matrix b=');
disp('matrix b')
disp(b);
s=a+b;
disp('addition of matrix=');
disp(s);

Prepared By

Dayananda Gowda K.R Lecturer E&C Department

Page

Matlab 2nd sem

Dept Electronics & Communication S.E.T.Polytechnic Melkote

Subtraction of two matrices.


clc;
clear all;
close all;
a=input('matrix a=');
disp('matrix a')
disp(a);
b=input('matrix b=');
disp('matrix b')
disp(b);
s=a-b;
disp('substraction of matrix=');
disp(s);

Prepared By

Dayananda Gowda K.R Lecturer E&C Department

Page

Matlab 2nd sem

Dept Electronics & Communication S.E.T.Polytechnic Melkote

multiplication of two matrices.

clc;
clear all;
close all;
a=input('matrix a=');
disp('matrix a')
disp(a);
b=input('matrix b=');
disp('matrix b')
disp(b);
p=a*b;
disp('multiplication of matrix=');
disp(p);

Prepared By

Dayananda Gowda K.R Lecturer E&C Department

Page

Matlab 2nd sem

Dept Electronics & Communication S.E.T.Polytechnic Melkote

Write a program to compute roots of a quadratic equation ax2+bx+c=0


given a, b and c.

clc;
clear all;
close all;
disp('enter the value of coefficient ');
a=input ('a= ');
b=input('b= ');
c=input ('c= ');
X1=(-1*b+sqrt (b*b-4*a*c))/(2*a);
X2=(-1*b+sqrt (b*b-4*a*c))/(2*a);
disp ('roots are');
disp (X1);
disp(X2);

Prepared By

Dayananda Gowda K.R Lecturer E&C Department

Page

Matlab 2nd sem

Dept Electronics & Communication S.E.T.Polytechnic Melkote

Compute mean, median, standard deviation and variance of a set of data using formulae
and verify using built-in functions.
Calculation of mean,

clc;
clear all;
close all;
A=[6,11,7];
M=mean(A);
disp('mean=');
disp(M);

Calculation of median

clc;
clear all;
close all;
A=[1:11];
M=median(A);
disp('median=');
disp(M);

Prepared By

Dayananda Gowda K.R Lecturer E&C Department

Page

Matlab 2nd sem

Dept Electronics & Communication S.E.T.Polytechnic Melkote

Calculation of standard deviation

clc;
clear all;
close all;
A=[9, 2, 5, 4, 12, 7, 8, 11 ];
s=std(A);
disp('s=');
disp(s);

1. Absolute
value of
number.

clc;
close all;
clear all;
a=[-3+4i];
y=abs(a);
Prepared By

Dayananda Gowda K.R Lecturer E&C Department

Page

Matlab 2nd sem

Dept Electronics & Communication S.E.T.Polytechnic Melkote

disp(y);

Average of the numbers in 3x4 matrix.


clc;
close all;
clear all;
a=input('enter the matrix=');
disp('a=');
disp(a);
Prepared By

Dayananda Gowda K.R Lecturer E&C Department

Page

Matlab 2nd sem

Dept Electronics & Communication S.E.T.Polytechnic Melkote

M=mean(a);
disp(M);

Logarithm of a number.
clc;
close all;
clear all;
x=8;
disp('x=');
disp(x);
y=log(x);
disp('logarithm');
disp(y);
Prepared By

Dayananda Gowda K.R Lecturer E&C Department

Page

Matlab 2nd sem

Dept Electronics & Communication S.E.T.Polytechnic Melkote

1. Factorial of a single digit number.


clc;
close all;
clear all;
x=10;
y=factorial(10);
disp(y);

Prepared By

Dayananda Gowda K.R Lecturer E&C Department

Page

Matlab 2nd sem

Prepared By

Dept Electronics & Communication S.E.T.Polytechnic Melkote

Dayananda Gowda K.R Lecturer E&C Department

Page

You might also like