You are on page 1of 45

NAME- RITAM MALLICK

ROLL NO.- 19/CSE/110

PAPER NAME- IT Workshop

PAPER CODE- PCC-CS 392

ASSIGNMENT-7

• Create two row vectors x= [1 2 3 4 5 6], y= [3 -1 2 4 5 1]. Plot 2D


graph between (x, y).

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:-

• Create a vector x of values ranging from zero to 2π (incremented by


π/100), compute y=2cos(x), y1=cos(x), y2=0.5cos(x) and set the graph
with dashed line style for the first plot, solid line style for the second plot
and dotted line style for the third plot.

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:-

• Create row vector x from 0-10. Compute y=cos(x), y1=sin(x). Display


1*2 subplots between (x,y) and(x,y1).

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:-

• Create a vector t of values ranging from 0 to 2 incremented by 0.01,


compute y=a*sin(2πft), y1= a*cos(2πft), and y2=t where
a=amplitude and f=frequency. Display 3*1 subplots between (t,y),(t,y1)
and (t,y2).
CODE:-
t=0:0.01:2 a=5 f=4
y=a*sin(2*%pi*f*t)
y1=a*cos(2*%pi*f* t)

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.

• Change graph colors.

• Exporting data to png format. iv) Create a duplicate sheet in the

variable editor.

• Remove specific columns.

• Display mean, variance and standard deviation.

i) We will use ‘xls_open(file_path)’ to open the file xlsx_file.xlsx and read


the two vectors in column 1 and column 2 sequentially in ‘data matrix’
// content of xlsx_file.xlsx :- // 100, 200, 400
// 10, 20, 30 // data matrix: [100 200 400; 10 20 30]

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.

// let data variable be 2X3 matrix:-


// [100 200 400; 10 20 30]
• We will delete specific column (for every row so that matrix don’t get
spoiled) using for-loop to rewrite the required data in a new matrix and
overwrite the ‘data’ matrix with the new matrix.

• 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

between x and y(x).

i) dy/dx=e-x with y=0 for x=0 to 10 incremented by 0.5 ii)

dy/dx+e-x y =x2 with y=0 for x=0 to 10 incremented by 0.5


• CODE:-

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:-

function dx=f(x, y) dx=x^2-exp(-x

)*y
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:-
• Solve the first order differential equation dy/dx=-2x-y (y0=-1, x0=0)
and

display the value of y for the value of x=0.4.

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

• Solve the definite integral function 7x3-8x2-3x+3 and integrate it between

a=-0.8 and b=1.4.


CODE:-

a=-0.8 b=1.4 sol=integrate('7*x^3-8*x^2-

3*x+3','x',a,b); disp(sol)

OUTPUT:-

1.9433333

• Read data from csv file and write data into csv file.

CODE:-

// Reading (content of csvfile1.csv is 100) :- a=csvRead('

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')

• i) Read data from a text file.

ii) Write integer and character values in a text file.

• 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,

'%d %c',111,'t') mclose(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

name of second person.

• Add field address to the first structure.

• 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"

*/

You might also like