Output-: Figure (1) X 0:0.05:2 Y1 Exp (X) Plot (X, Y1) Figure (2) Y2 Exp (-X) Plot (X, Y2)

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 15

figure(1)

x=0:0.05:2;
y1=exp(x);
plot(x,y1);
figure(2)
y2=exp(-x);
plot(x,y2);

OUTPUT-

1
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
1

0.9

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2

clc
clear
x=-pi:pi/20:pi;
y1=sin(x);
y2=cos(x);
plot(x,y1,'b-');
hold on;
plot(x,y2,'k-');
hold off;
legend('sin x','cos x');

OUTPUT-
1
sin x
0.8 cos x

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
-4 -3 -2 -1 0 1 2 3 4

clc
clear
figure(1)
subplot(2,1,1)
x=-pi:pi/20:pi;
y=sin(x);
plot(x,y);
title('Subplot 1 title');
subplot(2,1,2)
x=-pi:pi/20:pi;
y=cos(x);
plot(x,y);
title('Subplot 2 title');
Subplot 1 title
1

0.5

-0.5

-1
-4 -3 -2 -1 0 1 2 3 4

Subplot 2 title
1

0.5

-0.5

-1
-4 -3 -2 -1 0 1 2 3 4

A=[5 -3 2;-3 8 4;2 4 -9];


B=[10;20;9];
x=A\B

OUTPUT-

x=

3.4442

3.1982

1.1868

>> C=[A B]

C=

5 -3 2 10
-3 8 4 20

2 4 -9 9

n=0;sum_x=0;sum_x2=0;
x=input('Enter first value: ');
while x>=0
n=n+1;
sum_x=sum_x+x;
sum_x2=sum_x2+x^2;
x= input('Enter next value: ');
end
x_bar=sum_x/n;
std_dev=sqrt((n*sum_x2-sum_x^2)/(n*(n-1)));
fprintf('The mean of this data set is %f\n',x_bar);
fprintf('The standard deviation is %f\n',std_dev);
fprintf('The no of data points is %f\n',n);

OUTPUT-

Enter first value: 3

Enter next value: 4

Enter next value: 5

Enter next value: -1

The mean of this data set is 4.000000

The standard deviation is 1.000000

The no of data points is 3.000000

clc
clear
n=0;sum_x=0;sum_x2=0;
x=input('Enter first value: ');
while x>=0
n=n+1;
sum_x=sum_x+x;
sum_x2=sum_x2+x^2;
x= input('Enter next value: ');
end
if n<2
disp('At least 2 values must be entered!!!');
else
x_bar=sum_x/n;
std_dev=sqrt((n*sum_x2-sum_x^2)/(n*(n-1)));
fprintf('The mean of this data set is %f\n',x_bar);
fprintf('The standard deviation is %f\n',std_dev);
fprintf('The no of data points is %f\n',n);
end

OUTPUT-

Enter first value: 3

Enter next value: 4

Enter next value: 5

Enter next value: -1

The mean of this data set is 4.000000

The standard deviation is 1.000000

The no of data points is 3.000000

clc
clear
sum_x=0;sum_x2=0;
n=input('Enter number of points: ');
if n<2
disp('At least 2 values must be entered!!!');
else
for ii=1:n
x= input('Enter value: ');
sum_x=sum_x+x;
sum_x2=sum_x2+x^2;
end
x_bar=sum_x/n;
std_dev=sqrt((n*sum_x2-sum_x^2)/(n*(n-1)));
fprintf('The mean of this data set is %f\n',x_bar);
fprintf('The standard deviation is %f\n',std_dev);
fprintf('The no of data points is %f\n',n);
end

OUTPUT-

Enter number of points: 3

Enter value: 3

Enter value: 4

Enter value: 5

The mean of this data set is 4.000000

The standard deviation is 1.000000


The no of data points is 3.000000
clc
clear
for ii=1:5
if ii==3
break;
end
fprintf('ii=%d\n',ii);
end
disp(['End of loop!']);

OUTPUT-

ii=1

ii=2

End of loop!

clc
clear
for ii=1:5
if ii==3
continue;
end
fprintf('ii=%d\n',ii);
end
disp(['End of loop!']);

OUTPUT-

ii=1

ii=2

ii=4

ii=5

End of loop!

clc
clear
for ii=1:3
for jj=1:3
if jj==3
break;
end
product=ii*jj;
fprintf('%d * %d= %d\n',ii,jj,product);
end
fprintf('End of inner loop\n');
end
fprintf('End of outer loop\n');

OUTPUT-

1 * 1= 1

1 * 2= 2

End of inner loop

2 * 1= 2

2 * 2= 4

End of inner loop

3 * 1= 3

3 * 2= 6

End of inner loop

End of outer loop

MEAN, MEDIAN,STD,MAX,MIN,SUM,PROD,TRAPZ,DIFF
clc
clear
x=[1 2 3 4 5];
A=[6 5 -2;7 4 -1;8 3 0;9 2 1;10 2 2];
disp('MAIN MENU')
disp('1.Mean 2.Median 3.Standard Deviation 4.Max 5.Min 6.Sum 7.Cumsum 8.Prod
9.Cumprod 10. Trapz 11. Cumtrapz 12. Diff ')
choice=input('Enter your choice')
switch choice
case 1
mean(x)
mean(A)
break
case 2
median(x)
median(A)
break
case 3
std(x)
std(A)
break
case 4
max(x)
max(A)
break
case 5
min(x)
min(A)
break
case 6
sum(x)
sum(A)
break
case 7
cumsum(x)
cumsum(A)
break
case 8
prod(x)
prod(A)
break
case 9
cumprod(x)
cumprod(A)
break
case 10
trapz(x)
trapz(A)
break
case 11
cumtrapz(x)
cumtrapz(A)
break
case 12
diff(x)
diff(x)
break
otherwise
error('invalid choice Please enter correct choice')
end

INTERPOLATION
1]

clc
clear
x=[0 .785 1.57 2.356 3.141 3.927 4.712 5.497 6.283];
y=[0 .707 1 .707 0 -.707 -1 -.707 -0];
xi=linspace(0,2*pi,50);
yi=interp1(x,y,xi,'cubic');
plot(xi,yi)
title('Cubic Interpolation')
xlabel('X-axis')
ylabel('Y-Axis')
Cubic Interpolation
1

0.8

0.6

0.4

0.2
Y-Axis

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7
X-axis

2]

clc
clear
x=[0 .785 1.57 2.356 3.141 3.927 4.712 5.497 6.283];
y=[0 .707 1 .707 0 -.707 -1 -.707 -0];
xi=linspace(0,2*pi,50);
yi=interp1(x,y,xi,'linear');
plot(xi,yi)
title('Linear Interpolation')
xlabel('X-axis')
ylabel('Y-Axis')
Linear Interpolation
1

0.8

0.6

0.4

0.2
Y-Axis

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7
X-axis

3]

clc
clear
x=[0 .785 1.57 2.356 3.141 3.927 4.712 5.497 6.283];
y=[0 .707 1 .707 0 -.707 -1 -.707 -0];
xi=linspace(0,2*pi,50);
yi=interp1(x,y,xi,'nearest');
plot(xi,yi)
title('Nearest Interpolation')
xlabel('X-axis')
ylabel('Y-Axis')
Nearest Interpolation
1

0.8

0.6

0.4

0.2
Y-Axis

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7
X-axis

4]

clc
clear
x=[0 .785 1.57 2.356 3.141 3.927 4.712 5.497 6.283];
y=[0 .707 1 .707 0 -.707 -1 -.707 -0];
xi=linspace(0,2*pi,50);
yi=interp1(x,y,xi,'spline');
plot(xi,yi)
title('Spline Interpolation')
xlabel('X-axis')
ylabel('Y-Axis')
Spline Interpolation
1

0.8

0.6

0.4

0.2
Y-Axis

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7
X-axis

5]

x=linspace(0,pi,30);
y=sin(x)+0.2*rand(size(x));
plot(x,y,'*')
1.4
data 1

1.2

0.8

0.6

0.4

0.2

0
0 0.5 1 1.5 2 2.5 3 3.5

You might also like