Professional Documents
Culture Documents
Assignment-7: Name-Ritam Mallick
Assignment-7: Name-Ritam Mallick
ASSIGNMENT-7
CODE:-
x=[1 2 3 4 5 6 ]
y=[3 -1 2 4 5 1]
plot2d(x,y)
GRAPH:-
• i) Create row vector x from 0 to 10.Compute y=x2+10x+15.Plot 2D
graph between (x, y). ii) Set a suitable title for the graph. iii) Set suitable
titles for x, y axes. iv) Set font size, color for x, y axes.
CODE:-
// i) x=0:10
y=x^2+10*x+15
// ii)
title( '2D graph' )
// iii)
xlabel( 'x-Axis')
ylabel( 'y-axis')
// iv) xlabel("X-Axis",'fontsize',5,'color','blue')
ylabel("Y-Axis",'fontsize',5,'color','g')
title("2D-Graph",'fontsize',5,'color','r') plot(x,y)
GRAPH:-
• Create a vector x of values ranging from zero to 2π (incremented by
π/100), compute sine of the values and plot the result. Label the axes and
add a title. Set plot color with red.
CODE:-
x=0:%pi/100:2*%pi
y=sin(x)
xlabel("X-Axis",'fontsize',5,'color' ,'blue')
ylabel("Y-Axis",'fontsize',5,'color' ,'g')
title("2D-Graph",'fontsize',5,'color' ,'m')
plot(x,y,'r')
GRAPH:-
• Create a vector x of values ranging from zero to 10 with 100 uniformly
spaced points, compute cosine of the values and plot the result with black
color and dotted line style with square marker.
CODE:-
x=linspace(0,10,100)
y=cos(x) plot(x,y,'k:s')
GRAPH:-
• Create a row vector x from 0-10, compute y=cos(x), y1=sin(x) and set
the graph with red color and dash dotted line style for first plot and green
color and dashed line style for the second plot.
CODE:-
x=0:10 y=cos(x)
y1=sin(x)
plot(x,y,'r-.',x,y1,'g--')
GRAPH:-
CODE:-
x=0:%pi/100:2*%pi
y=2*cos(x) y1=cos(x)
y2=0.5*cos(x)
plot(x,y,'--',x,y1,'-',x,y2,':')
GRAPH:-
• Create a vector x of values ranging from zero to 10 with 100
uniformly spaced points, compute cosine of the values and plot the result
with boundary values for x axis from 0 to 4 and y axis from -1 to 2.
CODE:-
x=linspace(0,10,100)
y=cos(x)
plot(x,y)
a=gca()
a.data_bounds=[0,-1;4,2]
GRAPH:-
• Create row vector x from 0-10. Compute y=cos(x), y1=sin(x). Plot the
2D graph with legends in the lower right corner.
CODE:-
x=0:10
y=cos(x)
y1=sin(x)
plot(x,[cos(x'),sin(x')],[3 2])
legends(['cos(x)';'sin(x)'],[3 2],opt='lr')
GRAPH:-
• Create a vector t of values ranging from zero to π with 20 uniformly spaced
points, compute cos(t),cos(2t),cos(3t) and plot the 2D graph with legends in
the upper right corner.
CODE:-
t= linspace(0,%pi,20)
y1=cos(t)
y2=sin(2*t)
y3=sin(3*t) plot(t, [cos(t'), cos(2*t'), cos(3*t')], [3 4 5])
legends(['cos(t)';'cos(2t)';'cos(3t)'],[3 4 5],opt=1)
GRAPH:-
CODE:-
x=0:10
y=cos(x)
y1=sin(x) subplot(1,2,1) plot(x,y) subplot(1,2,2) plot(x,y1)
GRAPH:-
y2=t
subplot(3,1,1)
plot(t,y)
subplot(3,1,2)
plot(t,y1)
subplot(3,1,3)
plot(t,y2)
GRAPH:-
• Create a vector x of values ranging from -2 to 5 with 50 uniformly
spaced points, compute y=f(x) and y=g(x) where f(x)=(x2+ 2x)e-x,
and g
(x) = sin (x/2). Set the graph with red color for the first plot and green
color for the second plot.
CODE:-
function y=f(x)
y=(x^2 +2*x)*exp(-x)
endfunction
function y=g(x)
y=sin(x/2)
endfunction
x=linspace(-2,5,50)
plot(x,f,'r',x,g,'g')
GRAPH:-
• Create row vector x having 30 linearly spaced values starting at -2 and
ending at 2. Create a row vector y having 30 linearly spaced values
starting at -3 and ending at 3.Compute Z=2X2-Y2. Plot 3D surface
between (X,Y,Z).
CODE:-
x=linspace(-2,2,30)
y=linspace(-3,3,30)
[X,Y]=meshgrid(x,y)
Z=2*X.^2-Y.^2
surf(X,Y,Z)
GRAPH:-
• Create row vector x having 100 linearly spaced values starting at -1 and
ending at 1. Create row vector y having 100 linearly spaced values starting at
-2 and ending at 2. Compute z=f(x,y) where f(x,y)=2x2+y2 .Plot 3D
surface between (x,y,z) using feval function.
CODE:-
function z=f(x, y)
z=2*x.^2+y.^2
endfunction
x=linspace(-1,1,
100)
y=linspace(-1,1,
100)
z=feval(x,y,f)'
surf(x,y,z)
GRAPH:-
• Create a pie chart with proportions of 1, 2, 5.
CODE:-
pie([ 1 2 5 ])
GRAPH:-
• Create row vector x from 1-10. Create row vector n=[8 6 13 10 6 4 16 7
8 5]. Display bar chart between (x,n).
CODE:-
x=1:10
n=[8 6 13 10 6 4 16 7 8
5]
bar(x,n')
GRAPH:-
• Create row vector x=[1 2 5]. Create column vectors n1=[5;10;5]
and n2=[6;8;7]. Display bar chart between (x,n1) and (x,n2).
CODE:-
x=[1 2 5]
n1=[5;10;5]
n2=[6;8;7]
bar(x,[n1,n2])
GRAPH:-
• i)Import data from excel and plot different graphs.
variable editor.
CODE:-
plot(data(1,1:$),data(2,1:$))
ii)
plot(data(1,1:$),data(2,1:$),'r')
• In ‘Graphic Window’ we will use: “ File> Export To… ’’ for exporting the
image in png format.
GRAPH:-
• In ‘sci-lab console’ we will first define a variable, let the variable be ‘data’
then, in ‘variable browser’ we’ll double-click on ‘data’ variable.
It will open an Excel sheet like interface, we will enter data into it to get the
desired ‘matrix’ of data in ‘data’ variable.
• CODE:-
disp(mean(data)) disp(variance(data))
disp(stdev(data))
OUTPUT:-
126.6667
23026.667 151.7454
• Solve following first order differential equations and plot the 2D graph
function dx=f(x, y)
dx=exp(-x)
endfunction
y0=0 x0=0
x=0:0.5:
10 sol =
ode(
y0,x0,x,f)
plot(x,sol)
xlabel('x')
ylabel('y')
title('y(x)
v/s x')
GRAPH:-
• CODE:-
)*y
endfunction
y0=0 x0=0
x=0:0.5:10
sol=ode(y0,x0,x,f)
plot(x,sol) xlabel('x'
) ylabel('y')
GRAPH:-
• Solve the first order differential equation dy/dx=-2x-y (y0=-1, x0=0)
and
CODE:-
function dx=f(x, y)
dx=-2*x-y
endfunction
y0=-1 x0=0
x=0.4
sol=ode(y0,x0,x,f) disp(
sol)
OUTPUT:-
-0.8109602
3*x+3','x',a,b); disp(sol)
OUTPUT:-
1.9433333
• Read data from csv file and write data into csv file.
CODE:-
D:\STD\HIT\sem3\FLAT\0Flat-lab(it)\c12(ass7)\csvfile1.csv') //
Output:- a
= 100
//Writing:- csvWrite(200,'D:\STD\HIT\sem3\FLAT\0Flat-
lab(it)\c12(ass7)\
csvfile1.csv')
• CODE:- file_txt=mopen('D:\STD\HIT\sem3\FLAT\0Flat-
lab(it)\c12(ass7)\textfile1.txt'
//Output: 1 mclose(file_txt)
//Output: 0
• CODE:-
file_txt=mopen('D:\STD\HIT\sem3\FLAT\0Flat-
lab(it)\c12(ass7)\textfile1.txt', ‘w’)
// Output: 2 mfprintf(file_txt,
// Output: 0
• i)Create structures of two persons with name, age. Display age of first
person and name of second person. ii) Change age of first person and
• CODE:-
// In Console:-
structure=struct('name',Ravi
,'age',20); structure(2)=
struct('name',Ravi,'age',21);
structure(1).age // Output:
20 structure(2).name
// Output: Ravi
• CODE:-
// In Console:-
structure=struct('name',Ravi,
'age',20); structure(2)=struct(
'name','Ravi','age',21);
structure(1).age=10; structure(2
).name=’Suman’;
structure(1
).age //
Output: 10
structure(2)
.name
// Output: Suman
• CODE:- // In Console:- structure=
struct('name',Ravi,'age',20);
structure(2)=struct('name',
'Suman','age',21);
structure(1).address=
'place1'; structure(1)
/*
// Output:-
ans =
name:
"Ravi"
age: 20
address:
"place1"
*/