0% found this document useful (0 votes)
9 views31 pages

Matlab 04

MATLAB is a high-performance software for numerical computation and visualization, particularly focused on matrix operations. It provides a user-friendly interface and extensive built-in functions for various mathematical computations, including linear algebra and graphics. The document outlines basic commands, array operations, and examples of using MATLAB for mathematical and graphical tasks.

Uploaded by

Md Rony
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)
9 views31 pages

Matlab 04

MATLAB is a high-performance software for numerical computation and visualization, particularly focused on matrix operations. It provides a user-friendly interface and extensive built-in functions for various mathematical computations, including linear algebra and graphics. The document outlines basic commands, array operations, and examples of using MATLAB for mathematical and graphical tasks.

Uploaded by

Md Rony
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/ 31

About MATLAB :

MATLAB, which is short for Matrix Laboratory, incorporates numerical


computation, symbolic computation, graphics, and programming. As the name
suggests, it is particularly oriented towards matrix computations, and it provides
both state-of-the-art algorithms and a simple, easy to learn interface for
manipulating matrices. In this tutorial, I will touch on all of the capabilities
mentioned above: numerical and symbolic computation, graphics, and
programming .

MATLAB:
MATLAB is a software package for high performance numerical computation and
visualization .it provides an interactive environment with hundreds of build in
function for technical computation,graphics and animation.
Best of all it also provides easy extensibilitywith its own high level programming
language. The name MATLAB stands for MATrixLABoratory.

Application in Mathematics:
MATLABs build in function provides excellent tolls for linear algebra
(computations,data analysis,optimization,numerical solution of ordinary
differential equations (ode’s),quadratic and many other type of scientific
computation.
There are numerous for 2-D and 3-D graphics as well as for animation. Also for
those who can not do with their fortran or c codes.

Normal MATLAB Normal MATLAB Normal MATLAB


command command command
Π pi Sine sin Sinh2x (sinh(x))^2
√x Sqrt(x) Cosine cos a+ib a+ib
ex Exp(x) Tangent tan √a+ib Sqrt( a+ib)
3
lnx Log(x) Secant sec √x (x)^(1/3)
xn x^n Sin-1 asin Tan-1 atan
× * Cos-1 acos Cot-1 acot
÷ / Sin2x (sin(x))^2 cosecant cosec
Goal:
(1)To learn how to open MATLAB.
(2)Do a few trivalcalculation.(3)Quit MATLAB.
MATLAB commands:
(1) Plot==creat a 2-D line plot.
(2) Axes==changes the aspect the ratio of x and y axes.
(3) X label==annotates the x axis.
(4) Y label==annotates the y axis.
(5) Title==puts a title on the plot.
(6) Print==prints a hardcopy of the plot .
What we are goint to learn:
*How to do simple arithmetic calculation.
* How to assign values to variables.
*How to suppers screen output.
*How to quit matlab.
Creating and working arrays and numbers:
How to close MATLAB :
>>quit → enter
Or,>>file →exit MATLAB
### Some example :
(1) Compute y = 73+lnπ cosx for a particular value of x .
Ans:
>> x=15;
>> y=7^3+log(pi)*cos(x)
y = 342.1304
(2)Compute 53/35-1
Ans:
>> 5^3/(3^5-1)
ans = 0.5165
(3) Compute 3(√5-1)/(√5+1)2-1.
Ans:>> 3*(sqrt(5)-1)/(sqrt(5)+1)^2-1
ans =-0.6459
(4)Find the area of a circle where radius=π1/5-1.
Ans:>> r = (pi)^(1/5)-1;
>> Area =pi*r^2
Write the command to calculate the followings :
(1)e5i→>>exp(i*5) (ans=0.2837 - 0.9589i)
(2)ln(e9)→>>log(exp(9)) (ans=9)
(3)log10(e5)→>>log10(exp(5)) (ans= 2.1715)
(4)log10(105)→>>log10(10^5) (ans= 5)
(5)eπ√163 → exp(pi*sqrt(163)) (ans= 2.6254e+17 )
(6)sin2π/4-cos2π/4
Ans:>> (sin(pi/4))^2+(cos(pi/4))^2
ans = 1
(7)If y=cosh2x-sinh2x with x=32π ,compute the result.
Ans:>> x=32*pi;
>> y=(cosh(x))^2-(sinh(x))^2
y= 0
(8)Find the value of 1+5i/1-5i.
Ans:>> (1+5i)/(1-5i)
ans = -0.9231 + 0.3846i

(9)Find the value of eiπ/4 ,(with & without Euler’s law and compare with them).
(without Euler’s law) Ans:
>>exp(i*pi/4))
Ans= 0.7071 + 0.7071i
(With Euler’s law) Ans:
>>cos(pi/4)+i*sin(pi/4)
ans = 0.7071 + 0.7071i
Here both results are same .
ARRAY:
An array is a list of numbers or expression arranged in horizontal row and vertical
columns.
*When an array has only one row or column then it is called vector.
*An array with M row and N columns is called a matrix of size (M*N).
What we are going to learn:
(1) How to create row and column vectors.
(2) How to create avector of n numbers linearly spaced between two given
numbers a and b.
(3) How to do simple arithmetic operation on vector.

HOW TO DO ARRAY OPERATION:


# .* term by term multiplication.
# ./ term by term division.
# .^ term by term exponential.
(5) How to use trigonometric functions with array argument.
(6) How to use elementary math function such as square root, exponential,
logarithmwith array argument.
Goal:
(1)To learn how to create array and vectors,and how to perform arithmetic and
trigonometric operation on them.

## Create a row vector with fourelement.


solution:
>> x=[1 2 3 4]
Ans: x =[1 2 3 4]
##Create a column vector with three element.
Solution:
>>x=[1;2;3]

Ans: x=[ ]

## Creat a vector x with 7 element linearly spaced between 0 to 70.


Solution:
>> X=linspace(0,70,7)
Ans: X = 0 11.67 23.3 35.0 46.67 58.3 70.0
## Compute this √xsinx andsinx√x ,and compare it.
Solution:-
>>x=linspace(0,10,5);
>>y=sqrt (x).*sin(x)
Ans :
y = 0 0.9463 -2.1442 2.5688 -1.7203

>>x=linspace(0,10,5);
>>z=sin(x).*sqrt(x)
Ans :
z = 0 0.9463 -2.1442 2.5688 -1.7203
Here Both results are same .
##.If m=0.5, c=-2, compute y=mx+c at the following x co-ordinates
X=0,1.5,3,4,5,7,8,10

Solution:-
>> m=.5;
>> c=-2;
>> x=[0,1.5,3,4,5,7,9,10];
>> y=m*x+c
y = -2.0000 -1.2500 -0.5000 0 0.5000 1.5000 2.5000 3.0000

## Creat a vector t with elements, t=1,2,3,4,5,6,7,8,9,10, now compute


the problem .
(1) x=t sint
(2) y=t-1/t+1
(3) z=sint2/t2
Solution:
(1)
>> t=linspace(1,10,10);
>> x=t.*sin(t)
x=
0.8415 1.8186 0.4234 -3.0272 -4.7946 -1.6765 4.5989 7.9149 3.7091
-5.4402
(2)
>> t=linspace(1,10,10);
>> y=(t-1)./(t+1)

y = 0 0.3333 0.5000 0.6000 0.6667 0.7143 0.7500 0.7778 0.8000


0.8182
(3)
>> t=linspace(1,10,10);
>> z=sin(t.^2)./(t.^2)
z = 0.8415 -0.1892 0.0458 -0.0180 -0.0053 -0.0275 -0.0195 0.0144 -
0.0078 -0.0051
## If t and x any array such that t=(1,2,3,……….10) and
x=(15,16,17,…………,25) then compute the two variable by addition or
subtraction .
Solution:
>> t=linspace(1,10,10);
>> x=linspace(15,25,10);
>> y=t+x
y = 16.0000 18.1111 20.2222 22.3333 24.4444 26.5556 28.6667 30.7778
32.8889 35.0000

>> z=t-x
z =-14.0000 -14.1111 -14.2222 -14.3333 -14.4444 -14.5556 -14.6667 -14.7778
-14.8889 -15.0000
Points on a circle:
All points with coordinates x=rcosѲ and y=rsinѲwhere r is a
constant lie on a circle with radius r,that is the satisfied equation
x2+y2=r2 .
## Draw the circle of unit radious by using the parametric equation of
unit circle:
x=cosθ ,y=sinθ , 0≤ θ ≤ 2π .
Solution:
>>theta=linspace(0,2*pi,100);
>>x=cos(theta);
>>y=sin(theta);
>>plot(x,y)
>>title('Circle of unit radious');
>>title('Circle of unit radious');
>>axis('equal');
>>xlabel('X-Axis');
>>ylabel('Y-Axis');
Figure:

## Plot y1=sint, y2=t and y3= t - + In the same diagram :


Solution:
>>t=linspace(0,2*pi,100);
>>y1=sin(t);
>>y2=t;
>>y3=t-((t.^3)./6)+((t.^5)./120);
>>plot(t,y1,t,y2,'--',t,y3,'o')
>>axis([0 5 -1 5]);
>>xlabel('x-axis');
>>ylabel('y-axis');
>>title('A Multiple Graph For Three Functions');
>>text(1.0,0,'sin(t)');
>>gtext('y = Sint');
>>gtext('y = t');
>>gtext('y = t - t^3/3! + t^5/5! ');
>>legend('sin(t)','Linear Approximation','5th Order App')
Figure:

##A simple sine plot :


plot y=sinx, 0 ,taking 100 linearly spaced points in the given
interval label the axes and give a title.
Solution:
>> x=linspace(0,2*pi,100);
>> y=sin(x);
>>plot(x,y);
>>xlabel('x axis');
>>ylabel('y axis');
>>title('Graph of sin(x)')
Figure :

## Line style:
Make the same plots as above but rather than displayeding the graphs
as a curve , show the unconnected data points, to display the data
points with small circles ,use plots (x,y,’o’).Now combine the two plots
with the command plot (x,y,x,y.’o’).to show the line through the data
points as well as distinct data points.
Example: As exponentially decaying sin plot;

## Plot y=e-0.4xsinx, 0 , talking 10,50 and 100 points in the


interval.
Ans:
>> x=linspace(0,4*pi,50);
>> y=exp(-0.4*x).*sin(x);
>>plot(x,y)
>>xlabel('X')
>>ylabel('Y')
>>title('the graph of exponential function')
Figure:
the graph of exponential function
0.6

0.5

0.4

0.3

0.2
Y

0.1

-0.1

-0.2
0 2 4 6 8 10 12 14
X

## Plot y1= Sin(t) ,y2 = Cos(t) and y3= Tan(t) in the same diagram :
Solution :
>>t=0:15:90;
>>y1=sin(t);
>>y2=cos(t);
>>y3=tan(t);
>>plot(t,y1,'o',t,y2,'--',t,y3,'*')
>>axis('equal');
>>xlabel('X-Axis');
>>ylabel('Y-Axis');
>>text(1.0,0,'cos(t)');
>>gtext('Sin(t)');
>>gtext('Cos(t)');
>>gtext('Tan(t)');
>>legend('sin(t)','cos(t)','tan(t)');
Figure:
## Overlay plot:
## Plot y1=Sin(t) , y2 = Cos(t),y3 =Sin(t)+Cos(t) in a same diagram :

Solution:
>>t=linspace(0,2*pi,100);
>>y1=sin(t);
>>y2=cos(t);
>>y3=sin(t)+cos(t);
>>plot(t,y1,'o',t,y2,'--',t,y3,'*')
>>title('Graph of the trigonometric function Sin(t) , Cos(t) and >>Sin(t)+Cos(t)');
>>axis('equal');
>>xlabel('x-axis');
>>ylabel('y-axis');
>>text(4.0,0,'sin(t)');
>>gtext('sin-curve');
>>gtext('cos-curve');
>>gtext('multi curve');
>>legend('sin(t)','cos(t)','multi graph')
Figure:

## plot y=cosx and z=1-x2/2+x4/4 for 0


same plot
Solution:

>>x=linspace(0,pi,100 )
>>y=cos(x);
>>z=1-x.^2/2+x.^4/2;
>>plot(x,y,x,z);
>>plot(x,y,x,z,'*');
>>xlabel('X');
>>ylabel('Y');
>>title('the graph of cosx and z');
>>legend('cos(x),'z');

Figure:

## Variable in workspace:
(1) Type “who” see the variables in your workspace.
(2) Type ‘’whos’’ to get more information
(3) Type *theta, x, y+ to see the value of Ѳ, x, y listed as these
commands .
(4) To transpose them type a single right quote (‘) after their name.
## Write a MATLAB command to convert the temperature from
Celcius to Fahrenhite .
Sol: >> c=8;
>> F=9*c/5+32
F =46.4000

MATRIX:
A matrix is entered row wise with consecutive elements of a row,
separated by a space or a command and the columns are separated by
semicolons . The entire matrix must be enclosed within square
brackets.
## Write a MATLAB input command for the following matrices.
1)A=[ ]
Ans:>>A=[1 2 3;4 5 6]
2) B=lnyCosy
5i+lnxSinx
Ans:>>B=[log(y)cos(y);5*i+log(x) sin(x)]

3)A=[ ]

Ans:>>A=[1 2 9;2 10 5;3 4 -5]

4)A=1/3 5.55sinx 9.35 0.097


2/x+2lnx 3 0 6.555
5x-23/55 x-3 xsinx √3
Ans:x=2;
>> A=[1/3 5.55*sin(x) 9.35 0.097;2/(x+2*log(x)) 3 0 6.555;(5*x-23)/55 x-
3 x*sin(x) sqrt(3)]
A =[ ]

## Indexing(or subscripting):
A(I,j)=refers to the element aij of the matrix.
A(m:n,k:l)=specifies rows m to n and columns k to l of a matrix A.
A(:,5:20)= Refers to the elements in columns 5 through 20 of all rows of
A.
A(2:10,:)= Refers to the elements in row 2 through 10 of all columns of
matrix A.
## Dimension:size(A) →gives the dimension of A [m,n]
B=[ ]
B(2,3)=5
B=[ ]
C(3,1;3)=[1 2 3]

Produces C=[ ]

## sample question:
# Use (A (3,1:3) such as command) to change the row and column.

## Enter the matrix A=[ ]

1)Find A21 , A32.


2) Re-place A33 by 9.
3) Create asubmatrix A taking rows from 2 to 3 and columns from 1 to
3.
4)Obtain a submatrix of A by taking rows from 2 to 3 and all columns.
Solution:
1)
>>A(2,1)
ans = 4
>>A(3,2)
ans = 8
2)
>>A(3,3)
ans = 7
3)>>A(2:3,1:3)
ans =[ ]
4)>>A(2:3,:)
ans =[ ]
## Appending a row or column :A can be easily appending to an
existing matrix provided the row has the same length of the rows of
existing matrix. The something goes for columns.
The command A=[A U] appends the column vector U to the column A.
While A =[A;V] appending the row vector V to the rows of A.
## A row or column of any size may be appended to null matrix.
If A=1 0 0
0 1 0
0 0 1
U=[5 6 7]
V=2
3
4
1)Appends U to the rows of A.
2)Appends V to the column of A
3)Appends u’ to the column of A.
Ans:
>> x=[A;u]
x=

2)ans;>> y=[A v]
>>y =[ ]

3)>> u'

ans = [ ]

>> Z=*A u’+

z =[ ]

4) Ans: A=[A u]
??? Error using ==>horzcat
CAT arguments dimensions are not consistent.
It is not possible in MATLAB, because the dimensions of the matrix are
not acceptable for this operation.
5)Ans:
>>T=[];
>>S=[1 6 9];
>>M=[T;S]
M=[1 6 9 ]
##Deleting a row or column :
Any row or columns of a matrix can be deleted by setting the row or
column to a null vector.
A(2,:)=*+→Delete the 2nd row a matrix A.
A(:,3)=*+→Delete the 3rd column of a matrix A.
A(:,3,5)=*+→Delete the 3rd through 5th column of a matrix A.
A(*1,3+,:)=*+→Delete the 1st and 3rd row of a matrix A.
A(:,*1,3+)=→Delete the 1st and 3rd column of a matrix A.
Sample example:
>>Q=[1 2 3 4 5;5 4 3 2 1;4 5 6 7 8;3 4 5 6 6;1 3 5 8 9]
Q=1 2 3 4 5
5 4 3 2 1
4 5 6 7 8
3 4 5 6 6
1 3 5 8 9
>>Q(v,:)
ans = 1 2 3 4 5
3 4 5 6 6
1 3 5 8 9
>>Q(:,v)
ans = 1 4 5
5 2 1
4 7 8
3 6 6
1 8 9
## Reshaping matrix(as a vector):
Write a matlab command s.t all the element of a matrix A can be strung
into single column vector b.
Ex:>> A=ones(2)
A=1 1
1 1
>> b=A(:)

b=1
1
1
1
## As a differently sized matrix:
If matrix A is an m×n matrix it can be reshaped into p×q matrix as long
as m×n=p×q ,with the command reshape (a,p,q).
## Example: Transform a 6×6 matrix A into a 7×4 or 5×10matrix .
Ans:>> A=ones(6)
A=1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
>>reshape(A,7,4)
Ans = 1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
Similarly>> reshape (A,5,10)
Transpose:
1)B=A’ produces B=AT if A is a real matrix
2)B=A’ produces B= AT if A is a real complex matrix.
## Utility matrix :eye(m,n)→returns an m×n matrix with 1st on the main
diagonal.
zeros(m,n)→returns an m×n matrix of zeros.
ones(m,n)→returns an m×n matrix of ones.
rand(m,n)→returns an m×n matrix of random numbers.
diag(V)→Generates a diagonal matrix with vector v on the diagonal.
Example:
v=[1 7 3 2 5];
>>diag(v)
ans = 1 0 0 0 0
0 7 0 0 0
0 0 3 0 0
0 0 0 2 0
0 0 0 0 5
## tril(A)→extract the lower triangular part matrix A of a.
##triu(A)→extract the upper triangular part matrix A of a.
## Matrix and array operation :
A+B → is valid if A and B are of same size.
A-B → is valid if A and B are of same size.
A*B → is valid if A number of column equal B number of rows.
A/B → is valid if (and equal A.B-1) for the same size square matrix A and B.
A^2 →makes sense only if A is a square matrix.
A+α(α sclar) →adds αto each element of A.
A*α(α*A) →multiplies of each element of A by α.
Array operation :If u , v are two vectors then,
u.*v → produces element by element multiplication.
u./v → produces element by element division.
u.^v→ produces element by element exponentiation.

## MATRIX FUNCTION :

Exp(A)→ produce a matrix with element e(aij)


Log(A) → produce a matrix with element ln(aij)
Log10(A) → produce a matrix with element Log10A (aij)
Sqrt(A) → produce a matrix with element √Aij
Expm(A) →find eA
Logm (A) → find elog (A)
Sqrt(A) → find √A
Det(A) → find determination of A
Inv(A) → determination of A-1
eig (A) → finds eiagense values of A in a column vector
[eigval,eigvec] =eig (A) → find eigentvectore of A in the matrix eigvec and the
eigvalues of A on the diagonal of the matrix eigval.
## Enter the following the three matrices
1 4 , 3 8, 2 7
(1)Compute A+B ,and B+A ,also compare the result .
(2) compute (A+B)+C, A+(B+C) , also A=[6 2] B = [2 1] and C =[ 4-5]
compare the result.
(3) For α=5 , compute α(A+B) and αA+αB , also compare the result.
(4) For α=5 , compute A*(B+C) and A*B+A*C , also compare the result.
(5) For sclersab=ac→b=c if a≠0 is that true for matrix ,cheek by MATLAB .
(A) Computing A*B and A*C
>> A=[6 2;1 4];
>> B=[2 1; 3 8];
>> C=[4 -5; 2 7];
>> A+B
(1)ans = 8 3
4 12
>> B+A
ans = 8 3
4 12
Here both results are same .
(2)>> (A+B)+C
ans = 12 -2
6 19
>> A+(B+C)
ans = 12 -2
6 19
(3)>>alpha=5;
>>alpha*(A+B)
ans = 40 15
20 60
>>alpha*A+alpha*B
ans = 40 15
20 60
(4)>> A*(B+C)
ans = 46 6
26 56
>> A*B+A*C
ans = 46 6
26 56
(6)>> A*B
ans = 18 22
14 33
>> A*C
ans = 28 -16
12 23
Gaussian elimination method:
In introductory linear algebra courses , we learn to solve a system of algebraic
equation by Gaussian elimination, this technique requires forming a rectangular
matrix that contains both the co efficient matrix A and the know vector b is an
augmented matrix.
Gauss Jordan reduction is then used to transformed the augmented matrix to the
so called row reduce echelon form. MATLAB has a built in function ,rrefdoes
thatprecisely this reduction I, e . transform the matrix to its row reduced echelon
form.
## Solve the following system of,By linear equation gauss Jordan method.
5x-3y+2z=5; -3x+8y+4z=10; 2x+4y-9z=4;
Solution:
>>A=[5 -3 2;-3 8 4;2 4 -9 ];
>>b=[5;10;4];
>> X=inv(A)*b
X =1.6902
1.5695
0.6287
## By using left division :

>>X=A\b
X = 1.6902
1.5695
0.6287
By using gauss Jordan elimination:

>> c=[A b];


>> g=rref(c)
g =1.0000 0 0 1.6902
0 1.0000 0 1.5695
0 0 1.0000 0.6287
By using creamers rule:

>>A(1:3,1)=b

A = 10 -3 2
20 8 4
9 4 -9
>>A1=A;
>>A=[5 -3 2;-3 8 4;2 4 -9 ];
>>x=det(A1)/det(A)
x = 1.6902
A(1:3,2)=b
A= 5 5 2
-3 10 4
2 4 -9
>>A2=A;
>>A=[5 -3 2;-3 8 4;2 4 -9 ];
>>y=det(A2)/det(A)
y = 1.5695
>>A(1:3,3)=b
A = 5 -3 5
-3 8 10
2 4 4

>>A3=A;
>>A=[5 -3 2;-3 8 4;2 4 -9 ];
>>z=det(A3)/det(A)
z =0.6287
## If a=(1 2 ;4 -3) and f(x)=2x3-4x+5 then find f(A).
Solution:
>> A=[1 2;4 -3];
>> g=2*A^3-4*A+5*eye(2)
g = -13 52
104 -117
## write down the compound to find the eigenvalues and eigenvectors of A.also
verify the Cayley Hamilton theorem:
Here A = 41 1
25 -2
1 1 2
Solution:
>>A=[4,1,1;2,5,-2;1,1,2]
>>[V,D]=eig(A)
>>P=poly(A)

V = -0.6124 -0.6124 0.7071


-0.7071 0.7071 -0.7071
-0.3536 0.3536 -0.0000
D = 5.7321 0 0
0 2.2679 0
0 0 3.0000

P = 1.0000 -11.0000 37.0000 -39.0000

2-D GRAPHICS

Plot(x,y)-Plots y vs x with a solid line.


Plot(x,y,’--’)-Plots y vs x with a dashed line.
Plot(x)-Plots the elements of x against their row index.

##Style line: The style option in the plot command is a character string
that consists of 1,2
or character specify the color and the line style. There are several color,
line and marker
style option.

Color style Line style Marker style


option option option
Y for yellow - for solid + for plus sign
R for red -- for dashed 0 for circle
M for : for doted * for asterisk
magenta
C for cyon -. For dash-dot . for point
G for green X for xmark
B for blue ^ for uptringle
W for white S for square
K for black D for diamond
## The style option is made up of either the color option, the line style
option or a combination of the two.
Examples:
1)Plot(x,y,’r’)-plot y vs x with a red solid line.
2)Plot(x,y,’:’)-Plot y vs x with a doted line.
3)Plot(x,y,’b--’)-Plot y vs x with a blue-dashed line.
4)Plot(x,y,’+’)-Plot y vs x unconnected points marked by +.

Label ,title,legend and other object:


xlabel→-annotates the x-axis.
ylabel→-annototes the y-axis.
title→ (‘put a title on the plot’)
legend→ (‘mark the several graph’)

## Plot y1=sin(t),y2=t, y3=t-t3/3!+t5/5!


Do the plot using line command.

Solution:

>> t=linspace(0,2*pi,100);
>> y1=sin(t);
>> y2=t;
>> y3=t-(t.^3)/6+(t.^5)/120;
>>plot(t,y1)
>>line(t,y2,'linestyle','--')
>>line(t,y3,'marker','o')
>>axis([0 5 -1 5])
>>xlabel('t')
>>ylabel('approximations of sin(t)')
>>title('graph of sin(t)')
Graph:
fun with sin(t)
5

4
approximations of sin(t)

-1
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
t

## OVERLAY PLOT :
The command to generate overlay plots:
Plot y1=sint,y2=t and y3=t-t3/3!+t5/5!
Solution:
>> t=linspace(0,2*pi,100);
>> y1=sin(y);
>> y1=sin(t);
>> y2=t;
>> y3=t-(t.^3)/6+(t.^5)/120;
>>plot(t,y1,t,y2,'--',t,y3,'o')
>>axis([0 5 -2 5])
>>xlabel('t')
>>ylabel('approximation of sin(t)')
>>title('fun with sin(t)')
>>text(3.5,0,'sin(t)')
>>gtext('linear approximation')
>>gtext('first 3 terms')
>>gtext('in taylor series')

Graph:
fun with sin(t)
5

3
approximation of sin(t)

0 sin(t)

-1

-2
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
t

## Do the same using ‘hold’ command:


>> t=linspace(0,2*pi,100);
>> y1=sin(t);
>>plot(t,y1);
>> hold on
>> y2=t;
>>plot(t,y2,'y');
>> y3=t-(t.^3)/6+(t.^5)/120;
>>plot(t,y3,'g');
>>axis([0 5 -1 5]);
>>xlabel('t');
>>ylabel('approximation of sin(t)');
>>title('graph of hold function')
>> hold off
Graph:
graph of hod function
5

4
approximation of sin(t)

-1
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
t

## DIFFERENTIAL EQUATION :

Normal command MATLAB command


D
D2

Dy

D2 y

## Find the inflection point of f(x)=x3-6x2+9x+5


Solution:
>>syms x
>> f=x^3-6*(x^2)+9*x+5;
>> f2=diff(f,2)
f2 =6*x - 12
>>inflectionpt=solve(f2)
inflectionpt= 2
>>double(inflectionpt)
ans = 2
## Write MATLAB command to solve the following differential
equation.
(1) =1+y2 ,y(0)=1
Solution:
>>syms y
>> y=dsolve('Dy=1+y^2','y(0)=1')
y =tan(pi/4 + t)

(2)Solve =- ,y(4)=3 also plot the solution curve.


Solution:
>>dsolve('Dy=-(x/y)','y(4)=3','x')
ans =2^(1/2)*(25/2 - x^2/2)^(1/2)
>> x=linspace(0,10,100);
>> y=-(x.^2+25).^(1/2);
>>plot(x,y)

Graph:
-5

-6

-7

-8

-9

-10

-11

-12
0 1 2 3 4 5 6 7 8 9 10

(3)Solve :
Solution:
>>syms x
>>dsolve('Dy=asin(2*x)/(1+(x^2))','y(0)=1','x')
ans =int(asin(2*y)/(y^2 + 1), y = 0..x) + 1

THE END

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy