EXPERIMENT 01
Installation of the scilab, overview, basic syntax,
mathematical operators, predefined constants,
built in functions.
Objectives
• To enhance the numerical computational skills of
prospective engineers using open source
software/ computer algebra system (CAS). • To
plot of graphs of complicated functions.
Program 1
Write scilab code to find the solution of following
problem.
• 7+8/2 CODE
clc clear
A=7+8/2 disp("A=",A)
OUTPUT:
• (7+8)/2 CODE
clc
clear A=(7+8)/2 disp("A=",A)
OUTPUT:
• ( 4+20*5)/7
CODE
clc clear A=(
4+2*5)/7 disp("A=",A)
OUTPUT:
• 271/3+ 320.2 CODE
// //
clc
clear
A=nthroot(27,3)+32^0.2
disp("A=",A)
OUTPUT:
Experiment-2
Complex number, Polynomials, Vectors
Objectives:
• To perform basic calculation on complex numbers, polynomials
and scalar variables in scilab.
•
Program-1
Write scilab code to find the solution of following
problem.
• 641/2 + e4 CODE
//
//
clc clear
A=nthroot(64,2)+exp(4)
disp("A=",A)
OUTPUT:
• 𝒔𝒊𝒏(𝝅)+ 𝒄𝒐𝒔𝟔𝟎𝟎
𝟔
CODE
//
clc clear
A=sin(%pi/6)+cosd(60
) disp("A=",A)
OUTPUT:
• 4!+ln2+log100 CODE
clc clear
A=factorial(4)+log(2)+log10(100)
disp("A=",A)
OUTPUT:
• (2+3i)(4+5i)
CODE
clc
clear A=(2+3
*%i)*(4+
5*%i) disp("A
=",A)
OUTPUT:
Program-2
• Create, save and execute the script file for
the following problem:
The radius of a circle is 2cm. Find its area.
CODE
//
//script file for area of a circle clc
clear
r=2;
A=%pi*r^2; disp("A=",A)
OUTPUT:
EXPERIMENT 03
Write a SCI Lab code for Matrix. Handling these data
structure using built in functions.
Objectives:
• To create 1-D and 2-D arrays and operations with arrays
on scilab
• Creating two dimensional arrays(Matrix)
• Creating a vector from a known list of numbers.
Program 1
To write a Scilab code to find the solution of
following problems:
1. (i) Create a row vector with 3 elements.
CODE
clc clear A=[2,4,8]
disp("A=",A
)
OUTPUT:
(ii) Create a column vector with 4 elements
CODE
// //
clc clear A=[2;5;4;8]
disp("A=",A)
OUTPUT:
2. By taking first term a=1 and the last term b=10 create a
one dimensional array :
• By taking the spacing between two consecutive
terms d=2
CODE
clc clear
A=[Link]
disp("A=",A
)
OUTPUT:
• By taking the number of terms n= 12 CODE
//
//
clc clear
A=linspace(1,10,12)
disp("A=",A)
OUTPUT:
3. Create two row vectors (one dimensional
arrays) a and b such that the following
operations are defined and hence find :
• 2A-3B CODE
clc clear
A=[5,8,9]
B=[6,7,9] C=2*A-3*B
disp("C=",C)
OUTPUT:
• 𝟐𝑨′ −𝟑𝑩′
CODE
clc clear
A=[5,8,9]
B=[6,7,9] C=2*A'-3*B'
disp("C=",C)
OUTPUT:
4. Create two matrices( two dimensional
arrays)A and B such that the following
operations are defined and hence : o 3A-
ABT
CODE
clc clear
A=[5,8,9]
B=[6,7,9]
C=3*A-A*B' disp("C=",C)
OUTPUT:
5. Create a matrix A so that the following
operations are defined and find:
• Determinant of A CODE
//
//
clc clear
A=[5,8,9;4,1,9;6,8, 5]
C=det(A) disp("C=",C)
OUTPUT:
• Inverse of A
CODE
//
//
clc clear
A=[5,8,9;4,1,9;6,8,5] C=inv(A)
disp("C=",C)
OUTPUT:
• Product of A and inverse of A CODE
clc clear
A=[5,8,9;4,1,9;6,8,5]
C=A*inv(A) disp("C=",C
OUTPUT:
6. Find the roots of following polynomials:
(i) x2-x-2=0
CODE
clc clear
A=[1,-1,-2] R=roots(A)
disp("R=",R)
OUTPUT:
(ii) x3+1=0.
CODE
// clc clear
A=[1,0,0,1]
R=roots(A)
disp("R=",R)
OUTPUT:
EXPERIMENT-4
Write a SCI lab code for Programming for Loops-Conditional
statements
OBJECTIVES:
• Input function, conditional statements, loops and user defined functions
Program-1
Write a Scilab code in a script file:
1. To find volume and total surface area of a cylinder using input
function. CODE:
// //
clc clear
//volume and total surface area of a
cylinder r=input("enter the value of r:")
h=input("enter the value of h:") //volume of
cylinder = pi*r^2*h V=%pi*r^2*h
disp("Volume of cylinder=",V) //total surface
area of cylinder TS=%pi*r*(r+h) disp("Total
surface of cylinder",TS)
OUTPUT:
2. To find whether an integer entered by user is odd or even, using if-
else-end command.
CODE:
clc clear
//find whether an integer entered by user is odd or
even A=input("Enter the number:") if modulo(A,2)==0
then disp("Even") else disp("Odd") end
OUTPUT:
3. To find whether a real number entered by user is negative, zero or
a positive using if- else if-else-end command.
CODE:
clc clear
//find whether a real number entered by user is negative, zero
or a positive
A=input("Enter the number:")
if A>0 then disp("Positive")
elseif A<0 then
disp("Negative") else
disp("Zero") end
4. To find the sum of first n natural numbers, using for loop.
OUTPUT:
CODE:
clc clear
//the sum of first n natural numbers, using for
loop. n=input("Enter the value of n:") sum=0; for
i=1:n
sum=sum+i end disp("sum of 1st n
natural number=",sum)
OUTPUT:
5. To find the sum of first n natural numbers, using
while
loop.
CODE:
//
//
clc clear
//find the sum of first n natural numbers, using while loop.
n=input("enter the value of n:") sum=0 i=1 while i<=n
sum=sum+i; i=i+1; end
disp("sum of 1st n natural number:",sum)
OUTPUT:
6. To find factorial of a number using for loop.
CODE:
//
//
clc clear
//find factorial of a number using for
loop n=input("Enter the value of n:") f=1;
for i=1:n f=f*i end disp("n!=",f)
OUTPUT:
7. To find factorial of a number using while loop.
CODE:
//
//
clc clear
//find factorial of a number using while loop
n=input("Enter the value of n:") f=1; i=1
while i<=n f=f*i i=i+1
end disp("n!=",f
)
OUTPUT:
8. To find first 20 terms of Fibonacci sequence using for
loop CODE:
//
//
clc clear
//find first 20 terms of Fibonacci sequence using for
loop s=zeros(1,20); s(1)=1; s(2)=1; for i=3:20
s(i)=s(i-1)+s(i-2) end
disp("first 20 terms of Fibonacci sequence:",s)
OUTPUT:
9. To find volume and total surface area of a cylinder using user defined
function CODE:
//
//
clc clear
//find volume and total surface area of a cylinder using user
defined function
r=input("Enter the value of radius=")
h=input("enter the value of height=") function
[V,TSA]=cone(r,h)
V=%pi*r^2*h TSA=%pi*r
disp("volume=",V)
disp("Total surface Area=",TSA) endfunction
[V,TSA]=cone(5,4)
OUTPUT:
EXPERIMENT-4
OBJECTIVES:
• To write a SCILAB -CODE for 2D plots of curves: parabola,
circle, ellipse and hyperbola
• To write a SCILAB -CODE for 3D plots of surfaces: Planes, cone,
Sphere, Cylinder, Paraboloid, Ellipsoid, and Hyperboloid.
Write a Scilab code in a script file:
1. To plot Parabola x2=4ay. Take focal length a=1
CODE:
// //clc clear clf
//To plot Parabola x^2=4ay. Take focal length a=1 a=1 x=linspace(-
10,10,100) y=x^2/4*a
plot(x,y,".r") xtitle("Parabola","X-axis","Y-axis")
OUTPUT:
2. To plot Circle. x2+y2=a2 . Take a=1 CODE:
// //clc clear clf
//To plot Circle. x2+y^2=a^2 . Take a=1
t=linspace(0,%pi*2,100) a=1 x=cos(t)
y=sin(t) plot(x,y,".r")
xtitle("Parabola","X-axis","Y-axis")
OUTPUT:
𝑥2 𝑦2
3. To plot Ellipse 𝑎2 + 𝑏2 =1.. Take a=4, b=3 CODE:
//
// //
clc clear clf a=4 b=3
//To plot Ellipse x^2/a^2 +y^2/b^2 =1.. Take a=4, b=3
t=linspace(0,%pi*2,100) x=a*cos(t) y=b*sin(t)
plot(x,y,"r") xtitle("Ellipse","X-axis","Y-axis")
OUTPUT:
4. To plot Hyperbola. 𝑎𝑥22 −𝑦𝑏22 =1, Take a=3, b=4
CODE:
// //clc clear clf
//To plot Hyperbola. x^2/a^2 -y^2/b^2 =1, Take a=3, b=4
a=3 b=4 function [z]=f(x,y) z=x^2/a^2y^2/b^2-1
endfunction x=linspace(-1,1,100) y=linspace(-
1,1,100) contour(x,y,f,1) xtitle("Hyperbola","X-
axis","Y-axis") OUTPUT:
5. To plot a Plane ax+ by +cz=d. Take a=b=-1, c=1,
d=4. z=4+x+y CODE:
// clc clear
clf
//To plot a Plane ax+ by +cz=d. Take a=b=-1, c=1, d=4. z=4+x+y function
z=f(x,y) z=(d-a*x-b*y)/c z=4+x+y endfunction a=-1 b=-
1 c=1 d=4 x=linspace(-2,2,100)
y=linspace(-2,2,100) z=feval(x,y,f) surf(x,y,z)
OUTPUT:
6. To plot (elliptical) paraboloid z/c= x2/a2 +y2/b2 .
Take a=b=c=1 CODE:
// //
clc clear clf
//To plot (elliptical) paraboloid z/c= x2/a2 +y2/b2 . Take a=b=c=1 function
z=f(x,y) z=x^2+y^2 endfunction a=1 b=1 c=1 x=linspace(-2,2,100)
y=linspace(-2,2,100) z=feval(x,y,f) surf(x,y,z)
OUTPUT:
. Take
7. To plot (elliptical ) Cone. a=b=c=1 CODE:
// //clc clear clf
//To plot (elliptical ) Cone. z/c=√(x^2/a^2 +y^2/b^2 ) . Take a=b=c=1
function z=f(x,y) z=c*sqrt(x^2/a^2 +y^2/b^2) endfunction
a=1
b=1 c=1 x=linspace(-2,2,100)
y=linspace(-2,2,100) z=feval(x,y,f)
surf(x,y,z)
OUTPUT:
8. To plot right circular Cylinder. x2+y2=a2 CODE:
// //clc clear clf
//To plot (elliptical ) Cone. z/c=√(x^2/a^2 +y^2/b^2 ) . Take
a=b=c=1 t=linspace(0,%pi*2,100) x1=linspace(0,4,100)
[T,X1]=meshgrid(t,x1) x=2*cos(T) y=2*sin(T) a=(X1) surf(x,y,a)
OUTPUT:
9. To plot Sphere x2+y2 +z2 =a2 , Take a=1 CODE:
// //clc clear clf
// To plot sphere x2+y2+z2=a2
r=linspace(0,360,100); th=linspace(-
90,90,100); a=1;
[R,Th]=meshgrid(r,th); Z=a*sind(Th);
X=a*cosd(Th)*cosd(R);
Y=a*cosd(Th)*sind(R); Ncolor=100;
surf(X,Y,Z)
OUTPUT:
// //
clc clear
clf //To
plot
Ellipsoi d
x^2/a^
2
+y^2/b
^2
+z^2/c
^2 =1 .
Take a=4,b=
3,c=2
functio n
z=f(x,y)
z=c*sqrt(1-x^2/a^2-y^2/b^2) endfunction
a=4 b=3 c=2 x=linspace(-a,2,100)
y=linspace(-b,2,100) z=feval(x,y,f) surf(x,y,z)
OUTPUT:
EXPERIMENT-5:
OBJECTIVES
To write a SCILAB -CODE
5.1. To find the error in estimated value (yest) of a function f(x) at a point x using its Taylor
series.
5.2. To compare a function f(x) and its Taylor series expansion using 2d plots.
EXCERCISES
Write a Scilab code in a script file:
5.1.1. To find the error in estimating the value of function f(x)=ex at x=1 using
its Taylor series expansion about origin.
CODE:
// //clc; clear; a=0 x=1 y=%e^x
yest=0 n=1
for i=[Link]n
yest=yest+x^i/factorial(i) end
disp(y) disp(yest)
error=abs(y-yest) disp(error)
OUTPUT:
5.1.2. To find the error in estimating the value of function f(x)=sinx at x=pi/2
using its Taylor series expansion about origin.
CODE:
// clc; clear; a=0
x=%pi/2
y=sin(x) yest=0
n=10 for
i=[Link]n
yest=yest+((-1)^i)*(x^(2*n+i)/factorial(2*n+1)) end
disp(y) disp(yest) error=abs(y-yest) disp(error)
OUTPUT:
5.2.1. To compare the function f(x)=ex and its Taylor series expansion about
origin by using 2d plots
CODE:
clc; clear; clf;
x=linspace(-10,10,100)
y=%e^x yest=0
n=10
for i=[Link]n yest=yest+x^i/factorial(i)
end plot(x,y,"r")
plot(x,yest)
OUTPUT:
5.2.2. To compare the function f(x)=sinx and its Taylor series expansion about
origin by using 2d plots
CODE:
clc; clear; clf;
x=linspace(-%pi,%pi,100)
y=sin(x) yest=0
n=1
for i=[Link]n yest=yest+((-1)^i)*(x^(2*i+1)/factorial(2*i+1))
end plot(x,y,"r")
plot(x,yest)
xtitle("Graph of sin(x).[Link] series")
OUTPUT:
EXPERIMENT-6:
OBJECTIVES:
To write a SCILAB -CODE
6.1. To find the Fourier half range cosine series of f(x) in Half-range (0, L).
6.2. To find the Fourier half range sine series of f(x) in Half-range (0, L).
EXCERCISES
Write a Scilab code in a script file:
6.1.1. To find the Fourier coefficients of the Half range cosine series of y=f(x)= x2
in (0,2) and compare the graph of the function and the series.
CODE:
clc clear
clf
L=input("enter value of L :") n=input("enter
value of n :") a0=(2/L)*integrate('x^2','x',0,L)
disp("a0",a0) for i=(1:n)
a(i)=(2/L)*integrate('x^2*sin(i*%pi*x/L)','x',0,L) end
disp("a",a) x=linspace(0,L,40)
y=x^2 series=a0/2 for
i=(1:n)
series=series+a(i)*sin(i*%pi*x/L) end
plot(x,y)
plot(x,series,"*r") xtitle("plot function vs its fourier","x-
axis","y-axis") legend("plot of function","plot of function by
fourier series")
OUTPUT:
6.1.2. To find the Fourier coefficients of the Half range sine series of y=f(x)= x2
in (0,2) and compare the graph of the function and the series.
CODE:
//
clc clear
clf
L=input("enter value of L :") n=input("enter
value of n :") a0=(2/L)*integrate('x^2','x',0,L)
disp("a0",a0) for i=(1:n)
a(i)=(2/L)*integrate('x^2*sin(i*%pi*x/L)','x',0,L) end
disp("a",a) x=linspace(0,L,40)
y=x^2 series=a0/2 for
i=(1:n)
series=series+a(i)*sin(i*%pi*x/L) end
plot(x,y)
plot(x,series,"*r")
xtitle("plot function vs its fourier","x-axis","y-axis") legend("plot of
function","plot of function by fourier series")
OUTPUT:
6.2.1. To find the Fourier coefficients of the Half range cosine series of y=f(x)= x
in (0, pi) and compare the graph of the function and the series.
CODE:
// //clc clear clf
L=input("enter value of L :") n=input("enter value of
n :") a0=(2/L)*integrate('x','x',0,L)
disp("a0",a0) for i=(1:n)
a(i)=(2/L)*integrate('x*cos(i*%pi*x/L)','x',0,L) end
disp("a",a) x=linspace(0,L,40)
y=x series=a0/2
for i=(1:n)
series=series+a(i)*cos(i*%pi*x/L)
end plot(x,y)
plot(x,series,"*r") xtitle("plot function vs its fourier","x-axis","y-
axis") legend("plot of function","plot of function by fourier
series")
OUTPUT:
6.2.2. To find the Fourier coefficients of the Half range sine series of y=f(x)= x in
(0, pi) and compare the graph of the function and the series.
CODE:
// //clc clear clf
L=input("enter value of L :") n=input("enter value of
n :") a0=(2/L)*integrate('x','x',0,L)
disp("a0",a0) for
i=(1:n)
a(i)=(2/L)*integrate('x*sin(i*%pi*x/L)','x',0,L) end
disp("a",a) x=linspace(0,L,40)
y=x series=a0/2
for i=(1:n)
series=series+a(i)*sin(i*%pi*x/L)
end plot(x,y)
plot(x,series,"*r") xtitle("plot function vs its fourier","x-axis","y-
axis") legend("plot
of function","plot of function by fourier series")
OUTPUT: