0% found this document useful (0 votes)
56 views4 pages

Matlab Assignment

The document contains MATLAB code to solve nonlinear equations using bisection method and Newton Raphson method. It includes code snippets to find the roots of polynomials and trigonometric functions within intervals using bisection and Newton Raphson method.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views4 pages

Matlab Assignment

The document contains MATLAB code to solve nonlinear equations using bisection method and Newton Raphson method. It includes code snippets to find the roots of polynomials and trigonometric functions within intervals using bisection and Newton Raphson method.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Zahid bin azam

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

% 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

(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: ');

% 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

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy