1. A practical work of Matlab
Bachelor in computer application (BCA)
JANA BHAWANA CAMPUS
Godavari Municipality, 11-Lalitpur
Project work submitted for the partial fulfillment of BCA Program
Tribhuwan University, Practical Examination 2076
Project Prepare by External Examiner
Name: Signatures:
Roll. No: Internal Examiners:
Faculty: BCA Name:
Semester: 2nd Signature:
2. Acknowledgement
We must take this opportunity to acknowledgement our sincerely gratitude to the Jana Bhawana
Campus for providingthis type of goldenopportunity by providingquality educationin the field of the
c programming which help student to build and broaden the basis concept of c programming
In addition, we would like to thank and express our gratitude to our c programming lab teachers Mr.
β¦β¦β¦β¦β¦β¦β¦β¦β¦β¦β¦β¦β¦β¦β¦β¦.. for being our supervisor and helping us by providing valuable idea and
suggestion to perform various types of programming in c language.
3. Table of Contents
MATLAB........................................................................................................................................ 5
Basic Matlab...................................................................................................................................5
Matlab as a calculator to perform simple arithmetic operations:....................................................5
Addition.......................................................................................................................................... 5
Matlab recognizes variables......................................................................................................... 6
Some Commands in Matlab......................................................................................................... 7
Algebraic Simplification/ Expand ............................................................................................... 8
Limit and continuity...................................................................................................................... 19
Derivatives of exponential, logarithmic, and trigonometric function........................................... 23
Maxima and minima ..................................................................................................................... 30
Integrate and its application.......................................................................................................... 32
Some graph..................................................................................................................................35
5. MATLAB
MATLAB is a fourth-generation programming language and numerical analysis
environment.
Uses for MATLAB include matrix calculations, developing and running algorithms,
creating user interfaces (UI) and data visualization. The multi-paradigm numerical computing
environment allows developers to interface with programs developed in different languages,
which makes it possible to harness the unique strengths of each language for various purposes.
MATLAB is used by engineers and scientists in many fields such as image and signal
processing, communications, control systems for industry, smart grid design, robotics as well as
computational finance.
Cleve Moler, a professor of Computer Science at the University of New Mexico, created
MATLAB in the 1970s to help his students. MATLAB's commercial potential was identified by
visiting engineer Jack little in 1983. Moler, Little and Steve Bangart founded MathWorks and
rewrote MATLAB in C under the auspices of their new company in 1984.
Basic Matlab
Introduction
MATLAB is a fourth-generation programming language and numerical analysis environment.
Uses of MATLAB include matrix calculation, development and running algorithms, creating
user interfaces (UI) and data visualization. The multi-paradigm numerical computing
environment allows developers to interface with programs develops in different languages,
which makes it possible to harness the unique strengths of each language for various purposes.
MATLAB is used by engineers and scientists in many fields such as image and signal
processing, communications, control systems for industry, smart grid design, robotics as well
computational finance.
Matlabas a calculatorto performsimplearithmeticoperations:
Addition
i) 5+7
ii) ans+15
Solution (Using Matlab)
>> 5+7
ans =
12
>>ans+15
ans =
27
a) Subtraction, multiplication and division
i) 12-81
6. ii) ans-sin(pi/6)
iii) ans*11
iv) ans/2
v) ans/sqrt(169)
Solution (Using Matlab)
>> 12-81
ans =
-69
>>ans-sin(pi/6)
ans =
-69.5000
>>ans*11
ans =
-764.5000
>>ans/2
ans =
-382.2500
>>ans/sqrt(169)
ans =
-29.4038
Matlab recognizes variables
a) find the value of y if y=mx+c where m= 7, x= 8 and c= 2.
b) find the acceleration of a bike which runs at a speed of 4m/s in 5 seconds where
acceleration =
velocity
time
Solution (Using Matlab)
a) >> m=7;
>> x=8;
>> c=2;
>> y=m*x+c
7. y =
58
b) >> velocity=4;
time=5;
acceleration=velocity/time
acceleration =
0.8000
Some Commands in Matlab
a) Format long 2.6789765
>>format long
2.6789765
ans =
2.678976500000000
b) Format short 2.6789765
>>format short
>> 2.6789765
ans =
2.6790
c) Format bank 2.6789765
>>format bank
>> 2.6789765
ans =
2.68
d) Format long e 9841468343
>>format long e
>> 9841468343
ans =
9.841468343000000e+09
e) Format short e 9841468343
>>format short e
>> 9841468343
8. ans =
9.8415e+09
f) Format rat 2.345, pi
>>format rat
>> 2.345
ans =
469/200
>>format rat
>>pi
ans =
355/113
Algebraic Simplification/ Expand
a) Simplify sec2x- tan2x
>> simplify (sec(x)^(2)-tan(x)^(2))
ans =
1
b) Expand (a-2)3
>>syms a
>> expand ((a-2)^3)
ans =
a^3 - 6*a^2 + 12*a - 8
c) Expand (2x+3y)2
>>syms x y
>>expand((2*x-3*y)^2)
ans =
4*x^2 - 12*x*y + 9*y^2
SolvingQuadraticand Simultaneous LinearEquations
a) 2x2-5x+3=0
>>syms x
9. >> solve (2*x^2-5*x+3==0)
ans =
1
3/2
b) Solve for x and y in the pair of equation:
2x-3y=-11
X+4y=22
>>syms x y
eq1=2*x-3*y==-11;
eq2=x+4*y==22;
sol=solve([eq1,eq2],[x,y]);
xvalue=sol.x
xvalue =
2
>>yvalue=sol.y
yvalue =
5
10. Graph Plotting
a) Plot a graph for y=3x+5
>> x=[-2:5];
>> y=3*x+5;
>> plot(y)
b) Plot a scatter for y=3x+5
>> x=[-2:2:6];
y=3*x+5;
plot(y)
x=[-2,6];
y=3*x+5;
scatter(x,y)
c) Y= -x (range of x 0:10:100)
>> x=[0:10:100];
y =-(x);
>>plot(x,y)
11. d) Y=x2
x = [0:10:100];
y = x.^2;
>>plot (x,y)
e) Y=x3
>> x = [-100:10:100];
y = x.^3;
plot (x,y)
12. f) Y=sinx (range of x: -4pi to 4pi)
x= [-4*pi:4*pi];
>> y= sin(x);
>>plot (x,y)
13. g) Combine y=sinx and y=cosx in the same plot
>> x=[-4*pi:4*pi];
>> y1=sin(x);
>>plot(x,y1)
>> hold on
>> y2=cos(x);
>>plot(x,y2)
>> hold off
CompositeandInverseFunctions
a) f=2x+1; g= 2x-1; find fog, gof, fof and fog
>>syms x
>> f=2*x+1;
>> g=2*x-1;
>>compose(f,g)
ans =
4*x - 1
>>compose(g,f)
ans =
4*x + 1
>>compose(f,f)
ans =
14. 4*x + 3
>>compose(g,g)
ans =
4*x β 3
b) Find the inverse of x, 2x2-1
>>syms x
>> f=x;
>>finverse(f)
ans =
x
c)
>>syms x
>> f=2*(x^2)-1
f =
2*x^2 - 1
>>finverse(f)
ans =
(2^(1/2)*(x + 1)^(1/2))/2
Conic section
a) Plot a circle with center (2,3) and radius 5
15. >>viscircles([2,3],5)
b) Plot a ellipse with equation:
π₯2
16
+
π¦2
9
= 1
>>ezplot('x^2/16+y^2/9=1')
c) Plot a parabola with equation: y=8x2 [x value -10, 10]
>> x= [-10:10];
17. 3 4 11
0 -2 0
13 8 4
>> B*A
ans =
44 26 28
-21 -11 -9
20 16 46
>> A*B
ans =
32 20 10
18 13 9
26 18 34
>> A'
ans =
1 0 7
2 -1 4
5 3 2
>>inv(A)
ans =
-2/9 16/63 11/63
1/3 -11/21 -1/21
1/9 10/63 -1/63
>>det(A)
ans =
63
1) Vector a= (2,4,4) and vector b=(1,2,2)
a) Find modulus of vector a
a= [2 4 4];
18. >> b= [1 2 2];
>>norm(a)
ans =
6
b) Find a unit vector perpendicular to vector b
b =[1 2 2];
>> b/norm(b)
ans =
0.3333 0.6667 0.6667
c) a.b
>> a= [2 4 4];
b= [1 2 2];
>> dot (a,b)
ans =
18
d) axb
>> a= [2 4 4];
b= [1 2 2];
>> cross (a,b)
ans =
0 0 0
e) Also show that axb and bxa are not equal to each other
a= [2,4,4];
b= [1,1,1];
>> cross (a,b)
ans =
0 2 -2
>> cross (b,a)
ans =
0 -2 2
19. Limit and continuity
MATLAB provides the limit function for calculating limits. In its most basic form, the limit
function takes expression as an argument and finds the limit of the expression as the independent
variable goes to zero.
1. Evaluate lim
π₯β3
π₯ + 5
>> syms x
>> limit(x+5,3)
ans =
8
2. Evaluate lim
π₯β0
(π₯3
+ 5)/(π₯4
+ 7)
>> syms x
>> limit ((x^3+5)/(x^4+7))
ans =
5/7
3. Evaluate lim
π₯β1
( π₯ β 3)/(π₯ β 1)
>> syms x
limit ((x-3)/(x-1),1)
ans =
NaN
4. Algebraic limit theorem provides some basic properties of limits. These are as follow:
lim
π₯βπ
(π( π₯) + π( π₯)) = lim
π₯βπ
π(π₯) + lim
π₯βπ
π(π₯)
20. lim
π₯βπ
(π( π₯) β π( π₯)) = lim
π₯βπ
π(π₯) - lim
π₯βπ
π(π₯)
lim
π₯βπ
(π( π₯). π( π₯)) = lim
π₯βπ
π(π₯). lim
π₯βπ
π(π₯)
lim
π₯βπ
(π( π₯)/π( π₯)) = lim
π₯βπ
π(π₯)/ lim
π₯βπ
π(π₯)
Let us consider two functions:
F(x)=(3x+5)/(x-3) g(x)=x2+1.
Let us calculated the limits of the functions as x tends to 4, of both functions and verify the basic
properties of limits using these two functions and Matlab.
>> syms x
>> f=(3*x+5)/(x-3);
>> g=x^2+1;
>> l1=limit(f,4)
l1 =
17
>> l2=limit(g,4)
l2 =
17
>> add=limit(f+g,4)
add =
21. 34
>>sub=limit(f-g,4)
sub =
0
>> Mult=limit(f*g,4)
Mult =
289
>> div=limit(f/g,4)
div =
1
5. Find left hand limit and right hand limit of f(x)=(x-3)/| π₯ β 3|
lim
π₯β3
π(π₯)
>> f=(x-3)/abs(x-3);
>> ezplot(f,[-1,5]);
23. MATLAB provides the diff command for computing symbolic derivatives. In its simplest form,
you pass the function you want to differentiate to diff command as an argument.
1. Compute the derivative of the function f(x)=3x2+2x-2.
>> syms x
>> f=3*x^2+2*x-2;
>> diff(f)
ans =
6*x + 2
Derivatives of exponential, logarithmic, and trigonometric function
1.
Function Derivative
Ca.x Ca.x.In c.a(In is natural logarithm)
ex ex
In x 1/x
Incx 1/x.Inc
Xx XX.(1+In x)
Sin(x) Cos(x)
Cos(x) -sin(x)
Tan(x) Sec2(x) or 1/cos2(x)or 1+tan2(x)
Cot(x) -csc2(x) or -1/sin2(x) or β(1+cot2(x))
Sec(x) Sec(x).tan(x)
Csc(x) -csc(x).cot(x)
>> syms x
>> y=exp(x)
y =
exp(x)
>> diff(y)
24. ans =
exp(x)
>> y=sin(x)
y =
sin(x)
>> diff(y)
ans =
cos(x)
>> y=cos(x);
>> diff(y)
ans =
-sin(x)
>> y=tan(x);
>> diff(y)
25. ans =
tan(x)^2 + 1
>> y=sec(x);
>> diff(y)
ans =
sin(x)/cos(x)^2
>> y=cot(x);
>> diff(y)
ans =
- cot(x)^2 - 1
>> y=csc(x);
>> diff(y)
ans =
-cos(x)/sin(x)^2
>> syms x c
>> syms a
>> y=c^(a*x)
26. y =
c^(a*x)
>> diff(y)
ans =
a*c^(a*x)*log(c)
>> diff(log(x))
ans =
1/x
>> syms x;
>> diff(log10(x))
ans =
1/(x*log(10))
2. F(x)=3x+5
>> syms x;
f=3*x+5;
diff(f)
ans =
27. 3
3. Y=(a+β π₯) (a-β π₯)
>> y=(a+x^(1/2)*(a-(x)^(1/2)));
>> diff(y)
ans =
(a - x^(1/2))/(2*x^(1/2)) - Β½
4. Find y'' if y=(2x+)/(x-1)
>> syms x
>> y=(2*x+1)/(x-1);
>> a=diff(y)
a =
2/(x - 1) - (2*x + 1)/(x - 1)^2
>> diff(a)
ans =
(2*(2*x + 1))/(x - 1)^3 - 4/(x - 1)^2
>> diff(y,2)
ans =
28. (2*(2*x + 1))/(x - 1)^3 - 4/(x - 1)^2
5. Find the 3rd order derivative of f(x)=5x4-3x2+5
>> syms x;
>> f=inline(5*x^4-3*x^2+5)
f =
Inline function:
f(x) = x.^2.*-3.0+x.^4.*5.0+5.0
>> diff(f(x),3)
ans =
120*x
6. Find the derivation of f(x)=x3-3x2+3x and plot in graph.
>> syms x
>> f=x^3-3*x^2+3*x;
>> ezplot(f,[0,2]);
29. >> diff(f)
ans =
3*x^2 - 6*x + 3
Preety(y)
2
3x-6x+3
7. Define the following mathematical function in Matlab using the inline command:
g(y)=2sin(piy)+3ycos(piy)
>> g=inline('2*sin(p*y)+3*y*cos(pi*y)','y')
g =
30. Inline function:
g(y) = 2*sin(p*y)+3*y*cos(pi*y)
8. In Exercise 7 above, differentiation the function g with respect to y.
syms y
>> diff(g(y),'y')
ans=
3*cos(pi*y)+2*pi*cos(pi*y)-3*y*pi*sin(pi*y)
Maxima and minima
If we are searching for the local maxima and minima for a graph, we are basically looking for the
highest or lowest points on the graph of the function at a particular locality, or for a particular
range of values of the symbolic variable.
For a function y=f(x) the point on the graph where the graph has zero slope are called stationary
points. In other words stationary points are where f'(x)=0.
To find the stationary points of a function we differentiate, we need to set the derivative equal to
zero and solve the equation.
1. Find the maxima n minima of y=2x3+3x2-12x+17.
>> syms x
>> y=2*x^3+3*x^2-12*x+17;
>> ezplot(y,[-2,2]);
32. 37
ans =
10
Integrate and its application
Integration deals with two essentially different types of problems.
In the first types, derivative of a function is given and we want to find the function. Therefore,
we basically reverse the process of differentiation. This reverse process is known as anti-
differentiation, or finding the primitive function, or finding an indefinite integration.
The second type of problems involves adding up a very large number of very small quantities
and then taking a limit as the size of the quantities approaches zero, while the number of terms
tends to infinity. This process leads to the definition of the function of the definite integral.
Definite integrals are use for finding area, volume, center of gravity, moment if inertial, work
done by a force, and in numerous their applications.
1. Integrate: β«
1
π2+π₯2
ππ₯
>> syms a x;
int(1/(a^2+x^2))
ans =
atan(x/a)/a
35. ans =
(2*(2*pi - 3))/pi^2
9. Find the Taylor series expansion for the function cos x up to eight terms.
>> taylor(cos(x),x,8)
ans =
cos(8) + (sin(8)*(x - 8)^3)/6 - (sin(8)*(x - 8)^5)/120 - sin(8)*(x - 8) - (cos(8)*(x - 8)^2)/2 +
(cos(8)*(x - 8)^4)/24
10. Find the Taylor series expansion for the function ex up to nine terms.
>> taylor(exp(x),x,9)
ans =
exp(9) + exp(9)*(x - 9) + (exp(9)*(x - 9)^2)/2 + (exp(9)*(x - 9)^3)/6 + (exp(9)*(x - 9)^4)/24 +
(exp(9)*(x - 9)^5)/120
Some graph
1. Plot sinx/x (-1,1)
>> f=sin(x)/x
f =
sin(x)/x
>> ezplot(f,-1,1)
37. 4. Plot: 6-2x-x2(-5,3)
>> ezplot(6-2*x-x^2,[-5,3])
Linear Programming Problem
1. Solve the system : maxf= 7x1+5x2
Subject to x1+2x2β€6, 4x1+3x2β€6,x1β₯0,x2β₯0.
38. >> f=[-7;-5];
b=[6;6;0;0];
A=[1 2; 4 3; -1 0; 0 -1];
[x,fmin]=linprog(f,A,b)
Optimal solution found.
x =
1.5000
0
fmin =
-10.5000
Hence, fmax=10.5 at x1=1.5 and x2=0.
2. Find the roots of the following simultaneous equations using the Gauss-seidel method.
20π₯ + π¦ β 2π§ = 17
3π₯ + 20π¦ β π§ = β18
2π₯ β 3π¦ + 20π§ = 25
>> f1=@(x,y,z) (1/20)*(17-y+2*z)
f1 =
function_handle with value:
@(x,y,z)(1/20)*(17-y+2*z)
>> f2=@(x,y,z) (1/20)*(-18-3*x+z)
39. f2 =
function_handle with value:
@(x,y,z)(1/20)*(-18-3*x+z)
>> f3=@(x,y,z) (1/20)*(25-2*x+3*z)
f3 =
function_handle with value:
@(x,y,z)(1/20)*(25-2*x+3*z)
>> x=0; y=0; z=0;
>> for i=1:5
f1(x,y,z);
f2(x,y,z);
f3(x,y,z);
x=f1(x,y,z);
y=f2(x,y,z);
z=f3(x,y,z);
end
>> x