KEMBAR78
MATLAB Programming Guide | PDF | Fahrenheit | Computer Program
0% found this document useful (0 votes)
707 views13 pages

MATLAB Programming Guide

The document contains 11 MATLAB programming examples. The programs cover topics including: 1) Calculating the area of a circle, exponential functions, trigonometry, and complex numbers. 2) Plotting a straight line given the slope and y-intercept. 3) Creating and manipulating vectors using operations like multiplication, division and exponents. 4) Plotting waveforms like a sine wave and exponential function. 5) Writing functions to calculate the factorial of a number, cross product of vectors, and geometric series. 6) Drawing shapes like a unit circle. 7) Creating a table to convert between Celsius and Fahrenheit temperatures.

Uploaded by

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

MATLAB Programming Guide

The document contains 11 MATLAB programming examples. The programs cover topics including: 1) Calculating the area of a circle, exponential functions, trigonometry, and complex numbers. 2) Plotting a straight line given the slope and y-intercept. 3) Creating and manipulating vectors using operations like multiplication, division and exponents. 4) Plotting waveforms like a sine wave and exponential function. 5) Writing functions to calculate the factorial of a number, cross product of vectors, and geometric series. 6) Drawing shapes like a unit circle. 7) Creating a table to convert between Celsius and Fahrenheit temperatures.

Uploaded by

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

PROGRAM NO 1

Aim: Write MatLab program for following.


A) AREA = Πr 2 (USING ARITHMETIC OPERATOR).
B) e = π π (150) (USING EXPONENTIAL OPERATOR).
C) y = Sin2 π /3 + Cos2 π /3 (USING TRIGONOMETRY OPERATOR).
D) y = Cos π /4 + i Sin π/4 (USING COMPLEX NUMBER).
E) y= log10(106) (USING LOGARITHMS OPERATOR).

Coding:
A) AREA = Πr 2 (USING ARITHMETIC OPERATOR).
r=input(‘enter the value of radius’)
Ans: area =pi * (r)^2

Out put/Result: r=5


Area= 78.5398

B) e = π π (150) (USING EXPONENTIAL OPERATOR).


Ans: exp = pi ^ ( pi *(150))

Out put/Result:
exp = 1.8895e+234

C) y = Sin2 π /3 + Cos2 π /3 (USING TRIGONOMETRY OPERATOR). Ans: y


= (sin ( pi / 3 ) ) ^ 2 + (cos ( pi / 3 ) ) ^ 2

Out put/Result: y=1

D) y = Cos π /4 + i Sin π/4 (USING COMPLEX NUMBER). Ans: y


= cos ( pi / 4 ) + ί * ( sin ( pi / 4 ) )

Out put/Result: y = 0.7071 + 0.7071i

E) y= log10(106) (USING LOGARITHMS OPERATOR).


Ans: y = log10( 10^6 )

Out put/Result: y=6

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING 1


Glossary:
help <command> - specify a command and the info about it will be given
lookfor <topic> - specify a topic and all commands that have related topics will be listed who -
lists current variables
cd primer - changes the working directory to primer (assuming it exists)
pwd - tell you which directory you are current working in
dir - lists files in that directory
clear all - clears variables from memory
format long - lets you see more significant digits

PROGRAM NO 2

Aim: Compute y- coordinates of a STRAIGHT LINE y = mx + c, where slope of line m =0.5,


intercept c= -2 and x- coordinates: x = 0 to 10 for 0.5 increments.

Competency: To understand how to calculate the coordinate of a straight line.

Coding:
x = [ 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5 8 8.5 9 9.5 10 ]; y = 0.5 * x
–2

Out put/Result:
y = [-2 -1.75 -1.5 -1.25 -1 -0.75 -0.5 -0.25 0 0.25 .5 .75 1 1.25 1.5 1.75 2]

Conclusion: The coordinate has been successfully obtained.

Software used: MATLAB 7.

Hardware used: Pentium-4 Processor.

Precautions: Perform the part carefully.

Viva Questions: 1. What is full form of MATLAB?


2. What is benefit of doing this programme through MATLAB?

PROGRAM NO 3

Aim: Create following vectors t with 10 elements 1 to 10.


a) x = t sin(t) [ A MULTIPLE VECTORS]
b) y = (t-1) / (t+1) [ A DIVIDE VECTORS]
c) z = [sin(t2)/ (t2)] [ A EXPONENTIAL VECTORS]

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING 2


Competency: To understand the significance of vectors.

Coding:
t=1:10
a) X = t. * sin (t)
b) Y = ( t – 1 ) ./ ( t – 2 )

c) Z = (sin ( t.^2) ./ (t.^2))

Conclusion: The vectors have been used successfully.

Software used: MATLAB 7

Hardware used: Pentium-4 Processor.

Precautions: Perform the part carefully.

Viva Questions: 1. What is a vector?


2. Explain multiplication, division and exponential vectors.

Glossary:
Vector- A Vector is a special case of matrix, with just one row or one column. It is entered the same
way as a matrix.

Element by element operations-


.* Element by element multiplication
./ Element by element left division
.^ Element by element exponentiation

PROGRAM NO 4

Aim: PLOT y = Sin x where 0 ≤ x ≤ 2π .

Competency: To understand the formation of a waveform.

Coding:

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING 3


x = linspace (0, 2*pi );
plot ( x , sin(x))
xlable (‘x’) , ylable(‘sin(x)’)

Out put/Result:

Conclusion: Successfully form of a waveform.

Software used: MATLAB 7

Hardware used: Pentium-4 Processor.

Precautions: Perform the part carefully.

Glossary:
linespace (a,b,n)- Generates a linearly spaced vector of length n from a to b.
plot (x,y)- Plot x vs y.

xlabel (‘x’)- Label the x-axis with x.


ylabel (‘y’)- Label the y-axis with y.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING 4


PROGRAM NO 5
Aim: PLOT y = e - 0.4 x Sinx where 0 x 4π
Competency: To understand the plotting of a polynomial.

Coding:

X= linspace(0,4*pi);
Y = exp(-.4*x).*sin(x);
plot (x,y)

Out put/Result:

0.6

0.5

0.4

0.3

0.2

0.1

-0.1

-0.2
0 2 4 6 8 10 12 14

Conclusion: The plotting has been done successfully.

Software used: MATLAB 7

Hardware used: Pentium-4 Processor.

Precautions: 1. Perform the part carefully.


2. Debug the programs properly.

Viva Questions: What do you mean by the term Plotting.


What do you mean by linespace.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING 5


Glossary:
linespace (a,b,n)- Generates a linearly spaced vector of length n from a to b.
plot (x,y)- Plot x vs y.

PROGRAM NO 6

Aim: Write a script file to draw a unit circle.

Competency: To understand the formation of a unit circle.

Coding:
% circle – A script file to draw a unit circle. %--
----------------------------------------------------
theta = linspace(0, 2*pi,100); x =
cos (theta);
y = sin (theta); plot (x,y);
axis(‘equal’); title(‘circle of
unit radias’)

Out put/Result:

Conclusion: Successfully form the unit circle.

Software used: MATLAB 7

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING 6


Hardware used: Pentium-4 Processor, 40GB hard disk, 256MB RAM, Keyboard, Monitor.

Precautions: Perform the part carefully.

Viva Questions: What do you mean by the term Plotting?


What do you mean by linespace?
What do you mean by title?

Glossary:
linespace (a,b,n)- Generates a linearly spaced vector of length n from a to b.
plot (x,y)- Plot x vs y.
title – puts a title on the plot.

PROGRAM NO 7

Aim: Write a function factorial to compute the factorial n! for any integer n.

Competency: To understand the formation of a function.

Coding:

% FACTORIAL : function to compute factorial n!


% call syntax :
% factn = factorial (n);
%---------------------------------------
n=5
factorial(n)

Out put/Result: 120

Conclusion: Factorial of integer has been successfully obtained by using a function.

Software used: MATLAB 7

Hardware used: Pentium-4 Processor.

Precautions: Perform the part carefully.

Viva Questions: What is In-build function?


What is user define function?
Glossary:
factorial (n) - function calculates the factorial of given variable n.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING 7


PROGRAM NO 8

Aim: Write a function factorial to compute the factorial n! for any integer n.

Competency: To understand the formation of a function.

Coding:

% FACTORIAL : function to compute factorial n!


% call syntax :
% factn = factorial (n);
%---------------------------------------
n=5
factorial(n)

Out put/Result: 120

Conclusion: Factorial of integer has been successfully obtained by using a function.

Software used: MATLAB 7

Hardware used: Pentium-4 Processor.

Precautions: Perform the part carefully.

Viva Questions: What is In-build function?


What is user define function?
Glossary:
factorial (n) - function calculates the factorial of given variable n.

PROGRAM NO 9

Aim: Write a function file crossprod to compute the cross product of two vectors u and v.

Competency: To understand the formation of a function.

Coding:

function w = crossprod(u,v);
% CROSSPROD: function to compute w =u × v for vector u & v.
% call syntax :
% w= crossprod(u,v);
% ---------------------------------------------------
If length(u)>3 / length(v)>3
error(‘ask eular.this cross prodct is beyond me.’)
end
w = [u(2)*v(3) – u(3)*v(2);

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING 8


u(3)*v(1) – u(1)*v(3);
u(1)*v(2) – u(2)*v(1)];
Examples

a = [1 2 3];
b = [4 5 6];
c = cross(a,b)

c=
-3 6 -3

d = dot(a,b)

d=
32

Out put/Result:

Conclusion: The entire operators have been used successfully.

Software used: MATLAB 7

Hardware used: Pentium-4 Processor, 40GB hard disk, 256MB RAM, Keyboard, Monitor.

Precautions: 1. Perform the part carefully.


2. Debug the programs properly.

PROGRAM NO 10

Aim: Write a function to compute the geometric series 1 + r +


r2 + r3 + ………+ rn for given r and n.

Competency: To understand the formation of a function.

Coding:

function s= gseriessum(r,n);
% GSERIESSUM: function to calculate the sum of a geometric series
% the series is 1+r+r^2+r^3+………+r^n.
% call syntax :
% s = gseriessum(r,n);
%-------------------------------------------------------------------------
nvector =0:n;
series = r.^n vector;
s =sum(series);

Out put/Result:

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING 9


Conclusion: The entire operators have been used successfully.

Software used: MATLAB 7

Hardware used: Pentium-4 Processor, 40GB hard disk, 256MB RAM, Keyboard, Monitor.

Precautions: 1. Perform the part carefully.


2. Debug the programs properly.

PROGRAM NO 11

Aim: Write a function that outputs a conversion table for Celsius and Fahrenheit.

Competency: To understand the formation of a function.

Coding:

function temptable = ctof (tinitial, tfinal);


% CTOF : function to convert temperature from C to F
% call syntax :
% temptable = ctof (tinitial, tfinal);

%--------------------------------------------------------------------------------------- C
= [ tinitial; tfinal ]’; % Create a column vector C F = (9/5)*C + 32; %
Compute corresponding F temptable = [ C F ] % make a two column matrix
of C & F

Out put/Result: Depend upon the input values.

Conclusion: The entire operators have been used successfully.

Software used: MATLAB 7

Hardware used: Pentium-4 Processor.

Precautions: Perform the part carefully.

PROGRAM NO 12

Aim: Write a function to computes the interest on your account for a given principle amount, period and
rate of interest.

Competency:

Coding:

function [capital,interest] = compound(capital,years,rate,timescomp);


%COMPOUND : function to comput the compound capital and the interest

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING 10


% call syntax :
% [capital,interest] =compound(capital,years,rate,timescomp); %-----------
----------------------------------------------------------------------------
x0 =capital; n=years; r=rate; k=timescomp; if
r>1
disp(‘check your interest rate.for 8% enter .08,not 8. ’ ) end
capital = x0*(1+r/k)^(k*n);
interest = capital – x0;

Out put/Result:

Conclusion: The entire operators have been used successfully.

Software used: MATLAB 7

Hardware used: Pentium-4 Processor, 40GB hard disk, 256MB RAM, Keyboard, Monitor.

Precautions: 1. Perform the part carefully.


2. Debug the programs properly.

PROGRAM NO 13

Aim: Check following linear algebra rule for three MATRIX A,B AND C of any ranks.
a) ADDITION COMMUTATIVE.
b) ADDITION ASSOCIATIVE.
c) MULTIPLICATION WITH A SCALAR DSTRIBUTIVE.
d) MULTIPLICATION WITH A MATRIX DSTRIBUTIVE

Competency:

Coding:

A=[ 2 6 ;3 9],B=[1 2;3 4],C=[-5 5; 5 3];

% ADDITION COMMUTATIVE
y=[A+B];z=[B+A];

Out put/Result:
y= [3 8;6 13] ,z= [3 8;6 13]

% ADDITION ASSOCIATIVE
L=[(A+B)+C];M=[A+(B+C)];

Out put/Result:
L =[-2 13; 11 16] , M=[-2 13,11 16]

% MULTIPLICATION WITH A SCALAR DSTRIBUTIVE

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING 11


R=5(A+B);T=5A+5B;

Out put/Result:
R=[15 40;30 65] , T=[15 40;30 65]

% MULTIPLICATION WITH A MATRIX DSTRIBUTIVE


U=[A*(B+C)];V=[A*B+A*C]

Conclusion: The entire operators have been used successfully.

Software used: MATLAB 7

Hardware used: Pentium-4 Processor, 40GB hard disk, 256MB RAM, Keyboard, Monitor.

Precautions: Perform the part carefully.

PROGRAM NO 14

Aim: Find the solution of following linear algebraic equations.


x+2y+3z=1
3x + 3 y + 4 z = 1
2x + 3 y + 3 z = 2

Competency:

Coding:

A=[ 1 2 3 ;3 3 4;2 3 3];


B=[1;1 ;2];

X=A/B;
C=A*X

Out put/Result:
C=

1.0000
1.0000
2.0000

Conclusion: The entire operators have been used successfully.

Software used: MATLAB 7

Hardware used: Pentium-4 Processor, 40GB hard disk, 256MB RAM, Keyboard, Monitor.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING 12


Precautions: 1. Perform the part carefully.
2. Debug the programs properly.

PROGRAM NO 15

Aim: Find Eigen values and eigenvector of a 3 X 3 matrix.

Competency:

Coding:

a= [1 2 3;4 5 6;7 8 9]
E=eig (a)
[eigvec, eigval]=eig (a)

Out put/Result:

E=

16.1168
-1.1168
-0.0000

eigvec =

-0.2320 -0.7858 0.4082


-0.5253 -0.0868 -0.8165
-0.8187 0.6123 0.4082

eigval =

16.1168 0 0
0 -1.1168 0
0 0 -0.0000

Conclusion: The entire operators have been used successfully.

Software used: MATLAB 7

Hardware used: Pentium-4 Processor, 40GB hard disk, 256MB RAM, Keyboard, Monitor.

Precautions: 1. Perform the part carefully.


2. Debug the programs properly.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING 13

You might also like