Matlab 02
Matlab 02
What is MATLAB?
MATLAB is a software package for high performance numerical computation’s and visualization.
If provides an interactive environment with hundreds of built-in functions for technical
computation, graphics, and animation. Best of all, it also provides easy extensibility with its own
high -level programming language. The name MATLAB stands for MATrix LABoratory.
Application in mathematics?
MATLAB’s built –in functions provides excellent tools for linear algebra computations, data
analysis, signal processing, optimization, numerical solution of ordinary differential
equations(ODEs), quadrature, and many other types of scientific computations. There are
numerous functions for 2-D and 3-D graphics, as well as for animation. Also, for those who
cannot do without their Fortran or C codes.
MATLAB is normally operated from within the MATLAB integrated development environment (IDE).We
can launch MATLAB in the Windows environment by double-clicking
on the shortcut on our desktop, or by selecting it from the Start | Programs menu.
The IDE is organized into a header menu bar and several different windows. Which windows
are displayed can be determined by checking or unchecking items under the Layout menu
on the HOME tab. Some important windows for working with MATLAB are:
Command window: This is the main interactive interface to MATLAB. To issue a MATLAB
command, type the command at the >> prompt and press Enter on the keyboard.
Workspace browser: Each variable defined in the current workspace is represented here.
The name, class (type), value, and other properties of the variable can be shown. Choose
which properties to show using the View—Choose Columns menu from the header menu
bar. A recommended set to display is: Name, Value, and Class. Double-clicking on a
variable brings up the Variable Editor window. The icon representing numbers is meant
to symbolize an array, i.e., a vector or matrix. MATLAB’s basic data type is the array—
a number is treated as a 1×1 array.
Current Folder browser: In Windows parlance, the current folder is the same as the current
directory. Without further instruction, MATLAB will save files in the current folder and
look for files in the current directory. The browser displays files and other directories
(folders) that reside in the current directory. Icons at the top of the browser allow the
user to move up a directory (folder) level or to create a new folder. Double-clicking on a
displayed folder makes it the current folder.
Page 2 of 50
Editor window: The MATLAB editor is where programs are written. Launch the Editor
window by typing “edit” in the Command window and pressing Enter. It doubles as
part of the debugger interface, which is covered in detail later. The editor “knows” the
MATLAB language and color codes language elements. There are many other convenient
features to aid code-writing.
Figures window: Graphics is one of the main tools for visualizing numerical quantities.
The results of executing graphics-related commands, such as those for plotting lines and
surfaces, are displayed in the Figures window.
Variable Editor: The value or values held in a particular variable are displayed in a
spreadsheet-like tool. This is particularly useful for arrays (matrices and vectors)..
M-files:M-files are standard ASCII text files,with a(.m) extension to the file name. Any progrom written
in a MATLAB editor is saved as M-files .These files are futher classified as
*Script files
*Function files
SCRIPT FILES-An M-file with a set of valid MATLAB commands is called a script file. To run a script file,
the filename (without the .m extension) is entered in the Command Window, just as any other MATLAB
command
FUNCTION FILES- An M-file which begins with a function definition line is called a function file.
2. TUTORIAL LESSONS
Goal:To learn to log on, invoke MATLAB, do a few trivial calculation, quit MATLAB, and log off.
Page 3 of 50
What you are going to learn
Standard numerical operations are denoted by the usual symbols, and a very large number of functions
are available. Some examples follow.
+ addition
- subtraction
* multiplication
/ division
ˆ exponentiation, e.g., 1.3ˆ3.2 is 1.33.2
sin(x) returns the sine of x
sind(x) returns the sine of x degrees
cos(x) returns the cosine of x
cosd(x) returns the cosine of x degrees
tan(x) returns the tangent of x
tand(x) returns the tangent of x degrees
atan(x) returns the inverse tangent of x
atand(x) returns the inverse tangent of x in degrees
acos(x) returns the inverse cosine of x
acosd(x) returns the inverse cosine of x in degrees
asin(x) returns the inverse sine of x
asind(x) returns the inverse sind of x in degrees
exp(x) returns ex
log(x) returns the natural logarithm of x
log10(x) returns the log10(x)
sqrt(x) returns the square root of x
abs(x) returns the absolute value of x
round(x) returns the integer closest to x
ceil(x) returns the smallest integer greater than or equal to x
floor(x) returns the largest integer less than or equal to x
isprime(n) returns true if n is prime
factor(k) returns prime factors of k
sign(x) returns the sign (1 or −1) of x; sign(0) is 0
rand returns a pseudorandom number between 0 and 1
rand(m) returns an m×m array of random numbers
rand(m,n) returns an m×n array of random numbers
Page 4 of 50
How to write in MATLAB?
# Tutorial lessons
Page 5 of 50
Example: >> 2+3
ans =5
>> 4-2
ans =2
>>2*3
Ans=6
>>9/3
Ans=3
>>10+4-2
Ans=12
>>5*6/2
Ans=15
EXERCISES:
Solution:
Page 6 of 50
Solution:
>> (3*(sqrt(5)-1)/(sqrt(5)+1)*2)-1
ans = 1.2918
Area=π -1
Solution:
>> r=pi^(1/3)-1;
>> A=pi*r^2
A = 0.6781
* ln(e3), log10( )
, and log10( ).
Solution:
>> exp(3)
ans =20.0855
>> log(exp(3))
ans =3
>> log10(exp(3))
ans =1.3029
>> log10(10^5)
ans =5
Solution:
>> EXP(PI*SQRT(163))
Page 7 of 50
ANS = 2.6254 E +017
*SOLVE 3X=17 FOR X AND CHECK THE RESULT. (T HE SOLUTION IS X = . YOU CAN VERIFY THE RESULT BY
DIRECT SUBSTITUTION .)
ANS=2.5789
ANS=0.5000
>>COS(PI)
ANS=-1
>> TAN(PI/2)
ANS=1.663 E +16
>>SIN2 2
ANS=1
>>Y=COSH2X-SINH2X,WITH X =32Π
ANS=0
4. COMPLEX NUMBERS:
SOLUTION: (1+3I)/(1-3I)
ANS=-0.8000+0.6000 I
Page 8 of 50
*EI CHECK THE EULEER ’S FORMULA E
IX
=COSX-ISINX BY COMPUTING THE RIGHT -HAND SIDE TOO. I.E COMPUTE
COS (Π/4)+ ISIN(Π/4).
SOLUTION: EXP(I*PI/4)
ANS=0.7071+0.7071 I
*EXECUTE THE COMMANDS EXP(PI/2*I) AND EXP(PI/2I). CAN YOU EXPLAIN THE DIFFERENCE BETWEEN THE
TWO RESULTS ?
SOLUTION: >>EXP(PI/2*I)
ANS=0.0000+1.0000 I
>>EXP(PI/2I)
ANS=0.0000-1.0000 I
NOTE THAT
I
EXP(PI/2*I)=E =COS( )+ISIN ( )=I
Lesson 2
Array: Array is a list of numbers or expressions arranged in horizontal rows and vertical
columns. when an array has only one raw or column, it is called a vector. An array with m rows
and n columns is called a matrix of size m × n.
Goal: To learn how to create arrays and vectors and how to perform arithmetic and
trigonometric operations on them.
*How to create a vector of n numbers linearly(equally) spaced between two given numbers a
and b.
.^ term-by-term exponentiation.
*How to use elementary math functions such as square root, exponentials, and logarithms with
array arguments.
>> x=[1 2 3]
x =1 2 3
>> y=[2;1;5]
y=
>> z =[2 1 0]; you can add (or subtract) a row vector to a column vector.
>> a=x+z
a=
3 3 3
>> b=x+y But you cannot add(or substract) a row vector to acolumn
Vector.
Page 10 of 50
>> a=x.*z you can multiply (or divide) the elements of two same-sized
vectors term by term with the array operator .*(./)
a=
2 2 0
>> b=2*a But multiply a vector with a scalar does not need any special operation (no dot
before the *)
b=
4 4 0
>> x=linspace(0,10,5) create a vector x with five elements linearly spaced between 0 and 10.
x=
Columns 1 through 3
0 2.5000 5.0000
Columns 4 through 5
>> y=sin(x); Trigonometric functions sin, cos, etc. as well as elementary math functions sqrt ,
exp, log, etc. operate on vectors term by term.
>> z=sqrt(x).*y
z=
Columns 1 through 3
0 0.9463 -2.1442
Columns 4 through 5
EXERCISES:
Page 11 of 50
1. Equation of a straight line: The equation of a straight line is y=mx+c , where m and c are
constants. Compute the y-coordinates of a line with slope m=o.5 and the intercept c=-2 at the
following x-coordinates.
x=0, 1.5, 3, 4, 5, 7, 9, and 10
solution:
>> x=[0 1.5 3 4 5 7 9 10];
>> y=0.5*x-2
y=
-2.0000 -1.2500 -0.5000 0 0.5000 1.5000 2.5000 3.0000
*z=
Solution:
>> t=1:10;
>> x=t.*sin(t);
>> y=(t-1)/(t+1);
>> z=sin(t.^2)./(t.^2);
3.Points on a circle : All points with coordinates x=cosθ and y=sinθ, where r is a constant, lie on
a circle with radius r,i.e, they satisfy the equation x2+y2=r2. Create a column vector for θ with
the values 0, , , , π, .Take r=2 and compute the column vectors x and y . Now check that x
and y indeed satisfy the equation of a circle , by computing the radius r=√(x 2+y2).
Solution:
>> theta=[0;pi/4;pi/2;3*pi/2;pi;5*pi/4];
>> r=2;
>> x=r*cos(theta);
>> y=r*sin(theta);
>> r=sqrt(x.^2+y.^2)
r=
2
2
2
2
Page 12 of 50
2
2
4. Geometric series:
The sum of a geometric series 1+r+r2+r3+……..+rn approaches the limit for r<1as n α
a. Create a vector n of 11 elements from 0 to 10.
b. Take r=.5 and create another vector x=[r0 ,r 1, r2…rn].
c. Now take the sum of vector with the command s=sum(x).
d. Calculate the limit and compare the compute sum s.
Solution:
>> n=linspace(0,10,11);
>> r=.5;
>> x=r.^n;
>> s=sum(x)
s = 1.9990
>> p=1/(1-r)
p= 2
a) Repeat the procedure taking n from 0 to 50 and then from 0 to 100.
>> n=linspace(0,50,11);
>> x=r.^n;
>> =sum(x)
=2.0000
>>n=linspace(0,100,11);
>>x=r.^n;
>> =sum(x)
=2.00
Page 13 of 50
The MATLAB commands used are:
Plot creates a line plot,
axis changes the aspects ratio of the x axis and y axis,
xlabel annotates the x axis ,
ylabel annotates the y axis,
title puts a title on the plot, and
print prints a hard copy on the plot.
*You are requested to draws a circle of unit radious by using the parametric equations of a unit
circle.
X=r cos , y=r sin θ, 0 circle of unit radious
Solution: 0.8
>>x=cos(theta) 0.4
>>y=sin(theta);
0.2
plot(x,y)
>>xlabel('x') 0
y
>>ylabel('y') -0.2
>>axis(‘equal’) -0.4
>>title('circle of unit radious')
-0.6
-0.8
-0.2
-0.4
-0.6
Page 14 of 50
-0.8
-1
0 1 2 3 4 5 6 7
x
Figure: A simple sin plot
2.Line –styles: Make the same plot as above, but rather than displaying the graph as a curve,
show the unconnected data points.To display the data points with small circle, use polt(x,y,’0’).
Now combine the two plots with the command plot (x,y,x,y,’0’) to show the line through the
data points as well as the distinct data points.
Command:
plot created by Razik
>> x=linspace(0,2*pi,100); 1
plot(x,sin(x),x,sin(x),'+') 0.8
xlabel('x') 0.6
ylabel('sin(x)') 0.4
title('plot created by Razik') 0.2
sin(x)
-0.2
-0.4
-0.6
Figure: A simple line style
-0.8
-1
3.An exponentially decaying sine plot: Plot y=
0 1 sinx, 20 π 3 , taking
4 10,
5 50, and
6 1007
x
points in the interval. plot created by Mithun
0.6
Command:
>> x=linspace(0,4*pi,10); 0.5
>>y=exp(-0.4*x).*sin(x)
0.4
>>plot(x,y)
>>xlabel('x') 0.3
>>ylabel('y') 0.2
y
-0.1
Figure: An exponentially decaying sine plot
-0.2
0 2 4 6 8 10 12 14
x
0.2
y
ylabel('y')
title('plot created by Mithun')
20
,
15
10
0
1
0.5 1
0 0.5
0
-0.5
Figure:Space curve -0.5
-1 -1
5.Overlay plots: Plot y=cosx, and z=1- + for 0 on the same plot.You might like to
read to learn how to plot multiple curves on the same graph.
Command:
plot created by Antor
>> x=linspace(0,pi,100); 25
>>y=cos(x);
>>z=1-x.^2/2+x.^4/4; 20
>>plot(x,y,x,z)
>>plot(x,y,x,z,'+') 15
>>Xlabel('X')
>>Ylabel('Y') 10
Y
Page 16 of 50
0
-5
0 0.5 1 1.5 2 2.5 3 3.5
X
Figure: Overlay plots
6.Show the center of the circle: Modify the script file circle. m to show the center of the circle
on the plot, too. Show the center point with a "+".
Command : theta=linspace(0,2*pi,100) 1
x=cos(theta) 0.8
y=sin(theta) 0.6
plot(x,y,0,0,'+') 0.4
xlabel('X') 0.2
ylabel('Y')
0
Y
axis('qual')
-0.2
title('Plot created by RAZIK')
-0.4
-0.6
-0.8
Figure : Unit circle
-1
with the center point “+”
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
X
7.Variable in workspace: All variables created by a script file are left in the global workspace.
You can get information about them and access them, too.
*Type who to see the variables present in your workspace. You should see the variables r,
theta, x, and y in the list.
*Type whos to get more information about the variables and the workspace.
*Type *theta’ x’ y’+ to see the values of θ, x and y listed as three columns. All three variables are
row vectors. Typing a single right quote (‘) after their names transpose them and makes them
column vectors.
8. write a matlab command to convent the temperature from celsious to farenhiet
F= c+32
Command:
>> c=45;
>> f=(9/5)*c+32
Ans=
f=
113
10.write a MATLAB command to find the interest on 1000 at the rate of 6% a year at the end of
5 years if the interest is compound
a. annually
command:
>> p=1000;
>> n=5;
>> i=.06;
>> t=1;
>> A=p*(1+i/t)^t*n
A =5300
I. A=( )
Command: A = [1 2 5;3 9 0]
II. B=( )
III. C=( )
Page 18 of 50
IV. D=( )
# Indexing(or subscripting)
I. A(i,j)=refers to the element of of matrix A,i.e.,the element in the ith row and jth column.
Example: A(2,3)=
# Dimension
B(2,3)=5
C(3,1:3)=[1 2 3]
a) Find and
b) Replace by
c) Create a submatrix of a taking rows from 2 to 3 and columns from 1to 3
d) Obtain a submatrix of A taking rows from 2 to 3 and all columns.
Command
Page 19 of 50
b) A(2,3)
A(3,1)
c) A(3,3)=9
d) A(2:3,1:3)
e) A(2:3,:)
Any row or column of a matrix can be deleted by setting the row or column to a null vector.
*A([1 3], :)=[ ] deletes the 1st and the 3rd row of A, and
Page 20 of 50
# Reshaping matrices
Matrices can be reshaped into a vector or any other appropriately sized matrix;
As a vector: All the elements of matrix A can be strung into a single-column vector b by the
command b=A(:) (matrix A is stacked in vector b columnwise).
As differently sized matrix: If matrix A is an m×n matrix, it can be reshaped into a p×q matrix,
as long as m×n=p×q,with the command reshape (A,p,q).Thus,for a 6×6 matrix A,
Transpose
The transpose of matrix A is obtained by typing A’, i.e., the name of the matrix followed by the
single right quote. For a real matrix A, the command B=A’ produces B=A T and for a complex
matrix A, B=A’ produces the conjugate transpose B=AT.
Examples:
Utility matrices
To aid matrix generation and manipulation, MATLAB provides many useful utility matrices.For
example,
#Arithemetic operations
+ addition
- subtraction
* multiplication
/ division
^(caret) exponentiation
A+B is valid if A and B are of the same size.
A*B is valid if A’s number of columns equal and B’s number of rows.
Note: What will be happened if you use A*B, A.^2 etc in above.
#Array operation
Page 22 of 50
u./v produces element by element division.
Matrix functions
expm(A) = finds eA
sqrtm(A) =finds A
[eigvec,eigval]=eig(A) finds eigenvectors of A in the matrix eivec and the eigen values of on
the diagonal of the matrix eigval.
#Linear Algebra
1. Solve : 5x-3y+2z=10
-3x+8y+4z=20
2x+4y-9z=9
Page 23 of 50
>> x=A\b
Ax=b
Or, x=A-1b
# Gaussian elimination:
Introductory linear algebra courses, we learn to solve a system of linear algebraic equations
eliminations. This technique requires forming a rectangular matrix that contains both the
coefficient matrix A and the known vector b in an augmented matrix. Gauss-Jordan reduction
procedure is then used to transform the augmented matrix to the so-called row reduced
echelon form. MATLAB has a built in function, rref, that does precisely this reduction,
transforms the matrix to its row reduced echelon form Ax=b
5x-3y+2z=10
-3x+8y+4z=20
2x+3y-9z=9
Command:
>> b=[10;20;9];
cr =
1.0000 0 0 3.6610
0 1.0000 0 3.3995
0 0 1.0000 0.9467
Command:
>> f(A)=2*A^3-4*A+5*eye(2)
f=
83 76 159 152
*If A=( ) then write down the command to find the eigenvalues and eigenvectors of
command:
>> [V,D]=eig(A)
V=
-0.7071 -0.4472
Page 25 of 50
0.7071 -0.8944
D=
-1 0
0 5
>> p=poly(A)
p=
1 -4 -5
>> f(A)=A^3-11*A^2+39*A-45*eye(2)
f=
-64 32 -32 64
Command:
1.>> x=A+B
x=
3 8
6 18
Page 26 of 50
>> y=B+A
y=
3 8
6 18
2.>> p=(A+B)+C
p=
-2 13
11 21
>> q=A+(B+C)
q=
-2 13
11 21
3.>> alpha=5;
>> r=A*(B+C)
r=
40 86
60 129
>> t=alpha*A+alpha*B
t=
15 40
30 90
4.
>> v=A*(B+C)
v=
Page 27 of 50
40 86
60 129
>> m=A*B+A*C
m=
40 86
60 129
*Create the following matrixes with the help of matrix generation functions, zeros, eye, and
Command:
>> D=zeros(2,3)
>> E=eye(3)*5
>> F=ones(2)*3
Then
Command:
>> G=rand(10*10);
1.>> G(10,:)=[];
Page 28 of 50
2.>> G(:,10)=[];
3.>> G(1:4,1:4)=4
4. >> G(13);
If I type G(13) and hit return then I get the element of rows ‘3’ and columns ‘2’
6. >> G(12,1)=1;
If I type G(12,1)=1 and hit return then the elements of rows 12 and columns 1is replaced
by 1 and the other element of extra rows 11 ,12 is filled by 0.
Graphics
Plot (x) plots y vs x with the elements of x against their row index .
Style option:-The style option in the plot command in a character string that consists of
1,2,or character that specify the color and /or the line style. There are several color link
and marked style option.
The style option is made up of either the color option, the line style option or a
combination of the two.
*example:
Page 29 of 50
Plot(x,y,’r’) plot y vs x with a red solid line
*Axis control :
axis([-5 10 –inf 22]) sets the x axis from -5 to 10 and y axis limit to be set automatically
axis([-5 inf –inf 22]) sets the lower limit of x –axis and the upper limit of y-axis ,and leaves
the other two limits to be set automatically.
OVERLAY PLOTS
>> t=linspace(0,2*pi,100)
>>y1=sin(t)
>>y2=t;
>>y3=t-(t.^3)/6+(t.^5)/120
Page 30 of 50
>>plot(t,y1,t,y2,'--',t,y3,'+')
>>axis([0 5 -1 5])
>>xlabel('t')
>>ylabel('Approximination of sin(t)')
>>text(3.5,0,'sin(t)')
>>gtext('linear approximination')
>>gtext('first 3 term')
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)
axis([0 5 -1 5])
xlabel('t')
ylabel('Approximination of sin(t)')
title('rajeeb')
text(3.5,0,'sin(t)')
legend('sin(t)','linear approx','fifth-order approx.')
gtext('linear approximination')
gtext('first 3 term')
gtext('is taylor series')
Page 31 of 50
rajeeb
5
sin(t)
linear approx
4 fifth-order approx.
Approximination of sin(t)
3 linear approximination
first 3 term
2
is taylor series
0 sin(t)
-1
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
t
x=linspace(0,2*pi,100);
y1=sin(x);
plot(x,y1);
hold on
y2=x;
plot(x,y2,'--')
y3=x-(x.^3)/6+(x.^5)/120
plot(x,y3,'+')
axis([0 +5 -1 5])
xlabel('x')
ylabel('Approximination of sin(x)')
title('rajeeb')
text(3.5,0,'sin(x)')
legend('sin(x)','linear approx','fifth-order approx.')
gtext('linear approximination')
gtext('first 3 term')
gtext('is taylor series')
Page 32 of 50
rajeeb
5
sin(x)
linear approx
4 fifth-order approx.
linear approximination
Approximination of sin(x)
3
first 3 term
2 is taylor series
0 sin(x)
-1
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
x
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','+')
axis([0 5 -1 5])
xlabel('t')
ylabel('Approximination of sin(t)')
title('rajeeb')
text(3.5,0,'sin(t)')
legend('sin(t)','linear approx','fifth-order approx.')
gtext('linear approximination')
gtext('first 3 term')
gtext('is taylor series')
Page 33 of 50
rajeeb
5
sin(t)
linear approx
4 fifth-order approx.
linear approximination
Approximination of sin(t)
3 first 3 term
is taylor series
2
0 sin(t)
-1
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
t
****f(x)=xsinx,0≤x≤10π
Page 34 of 50
Fplot fplot('x.*sin(x)',[0 10*pi])
30
20
10
-10
-20
-30
0 5 10 15 20 25 30
y=t;
5
semilogx(x,y),grid
4
0
-3 -2 -1 0
10 10 10 10
t=linspace(0,2*pi,100);
semilogy(t,exp(t));
grid
2
10
1
10
0
10
0 1 2 3 4 5 6 7
Page 35 of 50
Loglog X= ,y=100+ ,0≤t≤2π 10
6
t=linspace(0,2*pi,200);
x=exp(t);
y=100+exp(2*t);
5
10
loglog(x,y),grid
4
10
3
10
2
10
0 1 2 3
10 10 10 10
Polar , 0≤t≤2π
t=linspace(0,2*pi,100); 90 1.5
120 60
r=sqrt(abs(2*sin(5*t)));
polar(t,r) 1
150 30
0.5
180 0
210 330
240 300
270
Page 36 of 50
Fill , 0≤t≤2π
1.5
x=rcost,y=rsint
t=linspace(0,2*pi,100); 1
r=sqrt(abs(2*sin(5*t)));
x=r.*cos(t); 0.5
y=r.*sin(t);
fill(x,y,'k'),
axis('square'); 0
-0.5
-1
-1.5
-1.5 -1 -0.5 0 0.5 1 1.5
plot3(x,y,z); 0.6
grid;
0.4
xlabel('X')
ylabel 0.2
0
1
1
0.8
0.5 0.6
0.4
0.2
0 0
X
Page 37 of 50
Surf √
Z=cosx cosy
,| | | |
u=-5:.2:5; 1
[X,Y]=meshgrid(u,u);
Z=cos(X).*cos(Y)...
0.5
exp(-sqrt(X.^2+Y.^2)/4);
surf(X,Y,Z)
0
-0.5
5
5
0
0
-5 -5
Surfc √
Z=cosx cosy
,| | | |
>> u=-5:.2:5;
[X,Y]=meshgrid(u,u);
Z=cos(X).*cos(Y).*exp(-
sqrt(X.^2+Y.^2)/4);
surfc(Z)
axis off
>>
Page 38 of 50
Mesh Z= ,| | | |
x=linspace(-3,3,50);
y=x; 0
[x,y]=meshgrid(x,y);
-1
z=-5./(1+x.^2+y.^2);
mesh(z) -2
-3
-4
-5
60
50
40
40
30
20 20
10
0 0
meshz Z= + y,
| |≤ | |
x=linspace(-3,3,50);
y=x;
[x,y]=meshgrid(x,y);
z=sin(x.^2)+sin(y.^2) 1
meshz(x,y,z),axis tight 0
view(-37.5,50) -1
3
2
3
1 2
0 1
-1 0
-1
-2
-2
-3 -3
Page 39 of 50
waterfall Z= ,| | | |
x=linspace(-3,3,50);
0
y=x;
[x,y]=meshgrid(x,y); -1
z=-5./(1+x.^2+y.^2);
-2
waterfall(z)
hidden off -3
-4
-5
60
50
40
40
30
20 20
10
0 0
comet Y=tsint,0
t=linspace(0,10*pi,2000); 25
y=t.*sin(t); 20
comet(t,y) 15
10
-5
-10
-15
-20
-25
0 5 10 15 20 25 30
Page 40 of 50
1.4
1.2
0.8
0.6
0.4
0.2
0
0 0.5 1 1.5 2 2.5 3
Barh plot:
cont=char('Asia','Europe','Africa','America','s.America');
pop=[3332;696;694;437;307];
barh(pop)
for i=1:5,
gtext(cont(i,:));
end
Xlabel('population in millions')
title('World population (1992)','fontsize',18)
Page 41 of 50
World population (1992)
s.America
5
America
4
3 Africa
2 Europe
1 Asia
Page 42 of 50
World population (1992)
6%
8%
s.America
America
13%
Africa
Asia
Europe
61%
13%
Differential Calculus:
Limit:
Page 43 of 50
1.Find | |
3.Find
exist
Solution:
1. syms x
limit(x/abs(x),0)
ans =
NaN
l.h.l=limit(x/abs(x),x,0,'left')
l=
h: [1x1 struct]
r.h.l=limit(x/abs(x),x,0,'right')
r=
h: [1x1 struct
2. l.h.l=limit((x+cos(x))/x,x,inf,'left')
l=
h: [1x1 struct]
r.h.l=limit((x+cos(x))/x,x,inf,'right')
r=
h: [1x1 struct]
limit((x+cos(x))/x,inf)
ans =
1
l.h.l=limit(x^2),x,1,'left')
l.h.l=limit((x^2),x,1,'left')
Page 44 of 50
l=
h: [1x1 struct]
3. l.h.l=limit((x+cos(x))/x,x,inf,'left')
l=
h: [1x1 struct]
r.h.l=limit((x+cos(x))/x,x,inf,'right')
r=
h: [1x1 struct]
limit((x+cos(x))/x,inf)
ans =
4.4. l.h.l=limit(x^2,x,1,'left')
l=
h: [1x1 struct]
r.h.l=limit(x^2+2,x,1,'right')
r=
h: [1x1 struct]
5. l .h.l=limit((x^2+1),x,0,'left')
l=
h: [1x1 struct]
r .h.l=limit(x,x,0,'right')
r=
Page 45 of 50
h: [1x1 struct]
f=
f1 =
cp =
1
1 - 2^(1/2)
2^(1/2) + 1
f2 =
12*x^2 - 24*x + 4
x=
f2 =
Page 46 of 50
12*x^2 - 24*x + 4
INTREGATE
Indefinite Intregate
a.∫ .
b. ∫ dx.
c .∫ dx.
d .∫
e .∫
Solution:
a .int(sin(x)*cos(x),x)
ans =
sin(x)^2/2
b .int((1-cos(2*x))/(1+cos(2*x)),x)
ans =
tan(x) – x
c .int(asin(x),x)
ans =
x*asin(x) + (1 - x^2)^(1/2)
d.int(1/((1+x^2)*atan(x)),x)
ans =
log(atan(x))
Page 47 of 50
e.int(1/(x^2+6*x+25),x)
ans =
atan(x/4 + 3/4)/4
Definite intregate
a.∫ dx .
b .∫ dx.
c .∫ dx.
d .∫ dx
e .∫
Solution:
aint(cos(2*x)*cos(3*x),x,0,pi/2)
ans =
3/5
b. int(sqrt(16-x^2),x,0,4)
ans =
4*pi
c. int(asin(x)/sqrt(1-x^2),x,0,1)
ans =
pi^2/8
Page 48 of 50
d .int(3*sqrt(1-cos(x))*sin(x),x,0,pi)
ans =
4*2^(1/2)
e. int(x/sqrt(4-x^2),x,0,1)
ans =
2 - 3^(1/2)
Differential Equation:
Write MATLB command to solve the following D.E.
1. =1+
dsolve('Dy=1+y^2')
ans =
i
-i
tan(C3 + t)
2. =1+ ,y(0)=1
>> y=dsolve('Dy=1+y^2','y(0)=1')
y=
tan(pi/4 + t)
>> dsolve('D2y+4*y=exp(-2*x)','y(0)=0','Dy(pi)=0','x')
ans =
Page 49 of 50
sin(2*x)/(8*exp(2*pi)) - cos(2*x)/8 - (sin(2*x)*(cos(2*x) - sin(2*x)))/(8*exp(2*x)) +
(cos(2*x)*(cos(2*x) + sin(2*x)))/(8*exp(2*x))
4.solve =- ;y(4)=3;
>> dsolve('Dy=-(x/y)','y(4)=3','x')
ans =
2^(1/2)*(25/2 - x^2/2)^(1/2)
***THE END***
Page 50 of 50