Matlab Assignment
Matlab Assignment
1813025
%prb1
%bisection code
(i)
% Input Section
y = x.^4-8*x.^3-35*x.^2+450*x-1001;
fplot(y,[-20 20])
a = input('Enter first guess: ');
b = input('Enter second guess: ');
e = input('Tolerable error: ');
(ii)
clc
% Setting x as symbolic variable
syms x;
% Input Section
y = -2*x.^6-1.5*x.^4+10*x+2
fplot(y,[-2 2.5])
a = input('Enter first guess: ');
b = input('Enter second guess: ');
e = input('Tolerable error: ');
newton Raphson
clc;
clear all;
syms x;
f= x.^4-8*x.^3-35*x.^2+450*x-1001;
g=diff(f); %The Derivative of the Function
n=input('Enter the number of decimal places:');
epsilon = 5*10^-(n+1)
x0 = input('Enter the intial approximation:');
for i=1:100
f0=vpa(subs(f,x,x0)); %Calculating the value of function at
x0
f0_der=vpa(subs(g,x,x0)); %Calculating the value of
function derivative at x0
y=x0-f0/f0_der; % The Formula
err=abs(y-x0);
if err<epsilon %checking the amount of error at each iteration
break
end
x0=y;
end
y = y - rem(y,10^-n); %Displaying upto required decimal places
fprintf('The Root is : %f \n',y);
fprintf('No. of Iterations : %d\n',i);
(ii)
clc;
clear all;
syms x;
f= -2*x.^6-1.5*x.^4+10*x+2;
g=diff(f); %The Derivative of the Function
n=input('Enter the number of decimal places:');
epsilon = 5*10^-(n+1)
x0 = input('Enter the intial approximation:');
for i=1:100
f0=vpa(subs(f,x,x0)); %Calculating the value of function at
x0
f0_der=vpa(subs(g,x,x0)); %Calculating the value of
function derivative at x0
y=x0-f0/f0_der; % The Formula
err=abs(y-x0);
if err<epsilon %checking the amount of error at each iteration
break
end
x0=y;
end
y = y - rem(y,10^-n); %Displaying upto required decimal places
fprintf('The Root is : %f \n',y);
fprintf('No. of Iterations : %d\n',i);
%prb2
%prb 3
(i)
clc
grid on
f=@(x)(sin(-2*x)-sin(2*x));
fplot(f,[-10 10])
r1=fzero(f,[-10 -9])
r2=fzero(f,[-9 -7])
r3=fzero(f,[-7 -5.5])
r4=fzero(f,[-5.5 -4])
r5=fzero(f,[-4 -2.5])
r6=fzero(f,[-2.5 -.5])
r7=fzero(f,[-.5 .5])
r8=fzero(f,[.5 2])
r9=fzero(f,[2 3.5])
r10=fzero(f,[3.5 6])
r11=fzero(f,[6 7])
r12=fzero(f,[7 8.5])
r13=fzero(f,[8.5 10])
(ii)
clc
grid on
f=@(x)((sin(-2*x).^3+(cos(4*x).^3)));
fplot(f,[-10 10])
r1=fzero(f,[-10 -9])
r2=fzero(f,[-9 -7])
r3=fzero(f,[-7 -5.5])
r4=fzero(f,[-5.5 -4])
r5=fzero(f,[-4 -2.5])
r6=fzero(f,[-2.5 -.5])
r7=fzero(f,[-.5 .5])
r8=fzero(f,[.5 2])
r9=fzero(f,[2 3.5])
r10=fzero(f,[3.5 6])
r11=fzero(f,[6 7])
r12=fzero(f,[7 8.5])
r13=fzero(f,[8.5 10])