Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Experiment number:01

# a=5

B=4

a*b=20

# x=30

Cos(x)=0.1543

sin(x)=-0.9880

tan(x)=-6.4053

# For Round of:

Y=16.89816

Sqrt(y)=4

log(y)=2.7726

exp(y)=808861e+06

round(y)=17

#Circle:

A=𝜋r2

r=5

pi*r^2=78.5398

# 3× 𝟑 Matrix:

124 1,1 1,2 1,3


A=[5 1 3] A=[124,513,211] |2,1 2,2 2,3|
211 3,1 3,2 3,3

A(2,2)=1

A(2,2)=0

A(2,2)=7
Experiment number:02

Experiment name: Plot of a function using MATLAB .

Plot the function y = x.

Program:

x = [0:5:100];

y = x;

plot(x, y)

Plot the function y = x2.

Program:

x = [1 2 3 4 5 6 7 8 9 10];

x = [-100:20:100];

y = x.^2;

plot(x, y)

Plot the sinx:

Program:

x = [0:0.01:10];

y = sin(x);

plot(x, y), xlabel('x'), ylabel('Sin(x)'), title('Sin(x) Graph'),

grid on, axis equal


Drawing multiple function on squre graph:

Program:

x = [0 : 0.01: 10];

y = sin(x);

g = cos(x);

plot(x, y, x, g, '.-'), legend('Sin(x)', 'Cos(x)')

Polynomials function:

Program:

x = [-10 : 0.01: 10];

y = 3*x.^4 + 2 * x.^3 + 7 * x.^2 + 2 * x + 9;

g = 5 * x.^3 + 9 * x + 2;

plot(x, y, 'r', x, g, 'g')

Setting Axis Scales:

Program:

x = [0 : 0.01: 10];

y = exp(-x).* sin(2*x + 3);

plot(x, y), axis([0 10 -1 1])


Generating Sub-Plots:

Program:

x = [0:0.01:5];

y = exp(-1.5*x).*sin(10*x);

subplot(1,2,1)

plot(x,y), xlabel('x'),ylabel('exp(–1.5x)*sin(10x)'),axis([0 5 -1 1])

y = exp(-2*x).*sin(10*x);

subplot(1,2,2)

plot(x,y),xlabel('x'),ylabel('exp(–2x)*sin(10x)'),axis([0 5 -1 1])
Experiment number: 03

Experiment name: MATLAB – Graphics.

Drawing Bar Charts:

Program:

x = [1:10];

y = [75, 58, 90, 87, 50, 85, 92, 75, 60, 95];

bar(x,y), xlabel('Student'),ylabel('Score'),

title('First Sem:')

print -deps graph.eps

Drawing Contours:

Program:

[x,y] = meshgrid(-5:0.1:5,-3:0.1:3); %independent variables

g = x.^2 + y.^2; % our function

contour(x,y,g) % call the contour function

print -deps graph.eps

modify

[x,y] = meshgrid(-5:0.1:5,-3:0.1:3); %independent variables

g = x.^2 + y.^2; % our function

[C, h] = contour(x,y,g); % call the contour function

set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2)

print -deps graph.eps


Experiment number:04

Experiment name: Bisection Method in MATLAB

Program:

a=input('Enter function with right hand side zero:','s');

f=inline(a);

xl=input('Enter the first value of guess interval:') ;

xu=input('Enter the end value of guess interval:');

tol=input('Enter the allowed error:');

if f(xu)*f(xl)<0

else

fprintf('The guess is incorrect! Enter new guesses\n');

xl=input('Enter the first value of guess interval:\n') ;

xu=input('Enter the end value of guess interval:\n');

end

for i=2:1000

xr=(xu+xl)/2;

if f(xu)*f(xr)<0

xl=xr;

else

xu=xr;

end

if f(xl)*f(xr)<0

xu=xr;

else

xl=xr;
end

xnew(1)=0;

xnew(i)=xr;

if abs((xnew(i)-xnew(i-1))/xnew(i))<tol,break,end

end

str = ['The required root of the equation is: ', num2str(xr), ‘ ‘]

Experiment number:05

Experiment name: MATLAB Code: Regular Falsi Method.

syms x;

% Input Section

y = input('Enter non-linear equations: ');

a = input('Enter first guess: ');

b = input('Enter second guess: ');

e = input('Tolerable error: ');

% Finding Functional Value

fa = eval(subs(y,x,a));

fb = eval(subs(y,x,b));

% Implementing Bisection Method

if fa*fb > 0

disp('Given initial values do not bracket the

root.');

else

c = a - (a-b) * fa/(fa-fb);

fc = eval(subs(y,x,c));

fprintf('\n\na\t\t\tb\t\t\tc\t\t\tf(c)\n');
while abs(fc)>e

fprintf('%f\t%f\t%f\t%f\n',a,b,c,fc);

if fa*fc< 0

b =c;

fb = eval(subs(y,x,b));

else

a =c;

fa = eval(subs(y,x,a));

end

c = a - (a-b) * fa/(fa-fb);

fc = eval(subs(y,x,c));

end

fprintf('\nRoot is: %f\n', c);

end

Enter non-linear equations: sin(x)+cos(x)+exp(x)-8

Enter first guess: 2

Enter second guess: 3

Tolerable error: 0.00001

a b c f(c)

2.000000 3.000000 2.010374 -0.054516

2.010374 3.000000 2.015152 -0.025119

2.015152 3.000000 2.017349 -0.011551

2.017349 3.000000 2.018358 -0.005306

2.018358 3.000000 2.018821 -0.002437

2.018821 3.000000 2.019034 -0.001119


2.019034 3.000000 2.019132 -0.000514

2.019132 3.000000 2.019177 -0.000236

2.019177 3.000000 2.019197 -0.000108

2.019197 3.000000 2.019207 -0.000050

2.019207 3.000000 2.019211 -0.000023

2.019211 3.000000 2.019213 -0.000010

Experiment number:05

Experiment name: Newton-Raphson Method.

Program:

syms x;

% Input Section

y = input('Enter non-linear equations: ');

a = input('Enter initial guess: ');

e = input('Tolerable error: ');

N = input('Enter maximum number of steps: ');

% Initializing step counter

step = 1;

% Finding derivate of given function

g = diff(y,x);

% Finding Functional Value

fa = eval(subs(y,x,a));

while abs(fa)> e

fa = eval(subs(y,x,a));

ga = eval(subs(g,x,a));

if ga == 0
disp('Division by zero.');

break;

end

b = a - fa/ga;

fprintf('step=%d\ta=%f\tf(a)=%f\n',step,a,fa);

a = b;

if step>N

disp('Not convergent');

break;

end

step = step + 1;

end

fprintf('Root is %f\n', a);

Enter non-linear equations: cos(x)-x*exp(x)

Enter initial guess: 1

Tolerable error: 0.00001

Enter maximum number of steps: 20

step=1 a=1.000000 f(a)=-2.177980

step=2 a=0.653079 f(a)=-0.460642

step=3 a=0.531343 f(a)=-0.041803

step=4 a=0.517910 f(a)=-0.000464

step=5 a=0.517757 f(a)=-0.000000

Root is 0.517757
Experiment number:06

Experiment name: Fixed Point Iteration Method using C Programming Language.

Program:

#include<stdio.h>

#include<conio.h>

#include<math.h>

/* Define function f(x) which

is to be solved */

#define f(x) cos(x)-3*x+1

/* Write f(x) as x = g(x) and

define g(x) here */

#define g(x) (1+cos(x))/3

int main()

int step=1, N;

float x0, x1, e;

clrscr();

/* Inputs */

printf("Enter initial guess: ");

scanf("%f", &x0);

printf("Enter tolerable error: ");

scanf("%f", &e);

printf("Enter maximum iteration: ");

scanf("%d", &N);

/* Implementing Fixed Point Iteration */


printf("\nStep\tx0\t\tf(x0)\t\tx1\t\tf(x1)\n");

do

x1 = g(x0);

printf("%d\t%f\t%f\t%f\t%f\n",step, x0, f(x0), x1,

f(x1));

step = step + 1;

if(step>N)

printf("Not Convergent.");

exit(0);

x0 = x1;

while( fabs(f(x1)) > e);

printf("\nRoot is %f", x1);

getch();

return(0);
Experiment number:07

Experiment name: Secant Method in MATLAB:

Program:

a=input('Enter function:','s');

f=inline(a)

x(1)=input('Enter first point of guess interval: ');

x(2)=input('Enter second point of guess interval: ');

n=input('Enter allowed Error in calculation: ');

iteration=0;

for i=3:1000

x(i) = x(i-1) - (f(x(i-1)))*((x(i-1) - x(i-2))/(f(x(i-1)) -

f(x(i-2))));

iteration=iteration+1;

if abs((x(i)-x(i-1))/x(i))*100<n

root=x(i)

iteration=iteration

break

end

end

Enter function: cos(x) + 2*sin(x) + x^2

f=

Inline function:

F(x) = cos(x) + 2*sin(x) + x^2

Enter first point of guess interval: 0

Enter second point of guess interval: -0.1


Enter allowed Error in calculation: 0.001

Root = -0.6593

Iteration = 6

You might also like