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

Experiment – 3 Program for scan conversion of a straight line.

OUTPUT:

Give coord[ x0 y0 x1 y1]: [0 0 5 5]


Experiment -4 Program for scan conversion of a circle.

OUTPUT:

Enter the coordinates as [x,y] : [0, 5]


Enter the radius : 4
Experiment -5 Program for scan conversion of an ellipse
OUTPUT:
Enter Horizontal radius : 3
Enter Vertical radius : 2
Enter the centere coordinates as [x,y] : [0, 0]
Experiment – 7 Program for finding roots of f(x)=0 by newton Raphson method
% Program Code of Newton-Raphson Method in MATLAB

a=input('Enter the function in the form of variable x:','s');


x(1)=input('Enter Initial Guess:');
error=input('Enter allowed Error:');
f=inline(a)
dif=diff(sym(a));
d=inline(dif);
for i=1:100
x(i+1)=x(i)-((f(x(i))/d(x(i))));
err(i)=abs((x(i+1)-x(i))/x(i));
if err(i)<error
break
end
end
root=x(i)

OUTPUT
Enter the function in the form of variable x:x^3-x-1
Enter Initial Guess:1.5
Enter allowed Error:0.001

f=

Inline function:
f(x) = x^3-x-1

root =

1.3252
Experiment No- 8 Write a program for bisection method in MATLAB

% Clearing Screen
clc
% Setting x as symbolic variable
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+b)/2;
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;
else
a =c;
end

c = (a+b)/2;
fc = eval(subs(y,x,c));
end

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


end

OUTPUT:
Enter non-linear equations: cos(x) - x * exp(x)
Enter first guess: 0
Enter second guess: 1
Tolerable error: 0.00001

a b c f(c)
0.000000 1.000000 0.500000 0.053222
0.500000 1.000000 0.750000 -0.856061
0.500000 0.750000 0.625000 -0.356691
0.500000 0.625000 0.562500 -0.141294
0.500000 0.562500 0.531250 -0.041512
0.500000 0.531250 0.515625 0.006475
0.515625 0.531250 0.523438 -0.017362
0.515625 0.523438 0.519531 -0.005404
0.515625 0.519531 0.517578 0.000545
0.517578 0.519531 0.518555 -0.002427
0.517578 0.518555 0.518066 -0.000940
0.517578 0.518066 0.517822 -0.000197
0.517578 0.517822 0.517700 0.000174
0.517700 0.517822 0.517761 -0.000012
0.517700 0.517761 0.517731 0.000081
0.517731 0.517761 0.517746 0.000035
0.517746 0.517761 0.517754 0.000011

Root is: 0.517757

Experiment -9 Program for solving numerical integration by Simpson’s 1/3 rule

OUTPUT:

Enter lower limit a: 1


Enter upper limit b: 2
Enter the number of sub-intervals n: 16

The value of integration is 4.352307

Experiment -10 Program for solving ordinary differential equation by Runge Kutta Method
OUTPUT:

Enter initial value of x i.e. x0: 0

Enter initial value of y i.e. y0: 0.5

Enter the final value of x: 2

Enter the step length h: 0.2

x y
0.000 0.500
0.200 0.632
0.400 0.838
0.600 1.133
0.800 1.538
1.000 2.077
1.200 2.780
1.400 3.683
1.600 4.829
1.800 6.274
2.000 8.083 >>

You might also like