0% found this document useful (0 votes)
82 views12 pages

Answer: Problem#1

The document presents code examples for using numerical methods to solve nonlinear equations, including the bisection method to bracket roots within lower and upper bounds, and the Newton-Raphson method to iteratively estimate roots starting from initial guesses. Functions and scripts are provided to implement the methods on sample equations

Uploaded by

Salman Abdullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views12 pages

Answer: Problem#1

The document presents code examples for using numerical methods to solve nonlinear equations, including the bisection method to bracket roots within lower and upper bounds, and the Newton-Raphson method to iteratively estimate roots starting from initial guesses. Functions and scripts are provided to implement the methods on sample equations

Uploaded by

Salman Abdullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Problem#1

Answer
Function file
function [root,iter]=bisection(func,xl,xu,es,maxit,varargin)

% bisect: root location zeroes

%[root,fx,ea,iter]=bisect(func,xl,xu,es,maxit):

%uses bisection method to find the root of func

% input:

%func = name of function

%xl, xu = lower and upper guesses

%es = desired relative error (default = 0.0001%)

%maxit = maximum allowable iterations (default = 50)

% output:

%root = real root

%fx = function value at root

%ea = approximate relative error (%)

%iter = number of iterations

if nargin<3,error('at least 3 input arguments required'),end

test = func(xl,varargin{:})*func(xu,varargin{:});

if test>0,error('no sign change'),end

if nargin<4 || isempty(es), es=0.0001;end

if nargin<5 || isempty(maxit), maxit=50;end

iter = 0; xr = xl; ea = 100;

while (1)

xrold = xr;

xr = (xl + xu)/2;
iter = iter + 1;

if xr ~= 0,ea = abs((xr - xrold)/xr) * 100;end

test = func(xl,varargin{:})*func(xr,varargin{:});

if test < 0

xu = xr;

elseif test > 0

xl = xr;

else

ea = 0;

end

if ea <= es || iter >= maxit,break,end

end

root = xr; fx = func(xr, varargin{:});

Script file

clear;

clc;

f = @(x) (x<=0).(x.^3 + 3*x +1) + (x>0).(1 + sin(x));

xL = -2;

xR = 0.1;

[root, iter] = bisection(f, xL, xR);

fprintf('The value of the root x*: %.4f\n', root)

fprintf('The value of f(x*): %.10f\n', f(root))

fprintf('The number of iterations performed: %d\n', iter)

Output
The value of the root x*: -0.3222
The value of f(x*): 0.0000001355

The number of iterations performed: 23

Problem#2
Answer
Script file

clear all

f=@(x)x.^3+3*x+1

df=@(x)3*x.^2+3

x0=input('enter initial guess')

while abs(f(x0))>0.00001

x1=x0-(f(x0)/df(x0))

x0=x1

end

Output file
f =

@(x) x .^ 3 + 3 * x + 1

df =
@(x) 3 * x .^ 2 + 3

enter initial guess> -2


x0 = -2
x1 = -1.1333
x0 = -1.1333
x1 = -0.5707
x0 = -0.5707
x1 = -0.3449
x0 = -0.3449
x1 = -0.3223
x0 = -0.3223
x1 = -0.3222
x0 = -0.3222
iter=5

f(-0.3222)= 1.3752 x 10^(-5)

Problem#3
Answer
Bisection Method
Script File
clc;

clear all;

close all;

err=10^(-5); %%% tolerence

N=20000; %%% max iteration

fx=@(x) 1/2+x^2/4-x*sin(x)-cos(2*x)/2;

a=0;

b=pi;

for n=1:N

c=(a+b)/2;

f1=feval(fx,a);
f2=feval(fx,b);

f3=feval(fx,c);

if ( f2 > f1 && f2 > f3 )

b=c;

else

a=c;

end

if (f3 < err )

break;

end

end

fprintf(' roots using bisection method is = %f with iteration %d \n',c,n);

Output File
roots using bisection method is = 1.570796 with iteration 20000

f(1.570796)=0.0460540184

Newton Method
Script File
clc;

clear all;

close all;

err=10^(-5); %%% tolerence

N=20000; %%% max iteration

% % % % using newton raphson method

fx=@(x) 1/2+x^2/4-x*sin(x)-cos(2*x)/2;

fdx=@(x) x/2-sin(x)+x*cos(x)+sin(2*x);

a(1,1)=pi/2;

a(2,1)=5*pi;

a(3,1)=10*pi;
%%

for m=1:3

for n=1:N

a(m,n+1)=a(m,n)-feval(fx,a(m,n))/feval(fdx,a(m,n));

k=abs(feval(fx,a(m,n+1)));

if (k<10^(-5))

break;

end

end

fprintf('With initial guess x0=%f roots is %f with iteration %d \n',a(m,1),a(m,n), n);

end

Output File
With initial guess x0=1.570796 roots is 1.891623 with iteration 440

With initial guess x0=15.707963 roots is 1.891623 with iteration 445

With initial guess x0=31.415927 roots is -0.006724 with iteration 55

F(1.891623)=1.000804x10^(-5)

F(1.891623)=1.000804x10^(-5)

F(-0.006724)=1.13027033x10^(-5)

Problem#4
Answer
Function File
function[r,itrr]=Bisectionf(f,xL,xR,tol)

% f=@(x)(x^2-7); %given

% a=input('\n Enter initial value of t i.e. a: ');

% b=input('\n Enter initial value of t i.e. b: ');


% while(f(a)*f(b)>0)

% fprintf('given value of a and b is not suitable for this method enter value of a and b such that
f(a)*f(b)<0');

% a=input('\n Enter initial value of t i.e. a: ');

% b=input('\n Enter initial value of t i.e. b: ');

% end

% tol=input('\n Enter tolerence allowed: ');

a=xL;

b=xR;

i=1;

x1=a;x=b; % just for going inside while loop for one time

itter_mat=[];% for ploting the ittreation value

ERR_mat=[];% FOR PLOTING ERROR AT EACH ITTERATION

while(abs(x1-x)>tol)

fprintf('iteration=%2d\n',i);

i=i+1;

x=(a+b)/2;

itter_mat=[itter_mat,x];

ERR_mat=[ERR_mat,abs(x-sqrt(2))];

fprintf('x=%2d\n',x)

if(f(x)*f(a)<0)

b=x;

else

a=x;

end

fprintf('iteration=%2d\n',i);

i=i+1;

x1=(a+b)/2;

itter_mat=[itter_mat,x1];
ERR_mat=[ERR_mat,abs(x1-sqrt(2))];

fprintf('x=%2d\n',x1)

if(f(x1)*f(a)<0)

b=x1;

else

a=x1;

end

end

r=x1;

itrr=length(itter_mat);

itter_mat

ERR_mat

plot(itter_mat)

hold on

plot(ERR_mat)

Script File
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 File
Enter non-linear equations: > x^2-7

Enter first guess: > -1

Enter second guess: > 9

Tolerable error: > 0.001


a b c f(c)
-1.000000 9.000000 4.000000 9.000000

-1.000000 4.000000 1.500000 -4.750000

1.500000 4.000000 2.750000 0.562500

1.500000 2.750000 2.125000 -2.484375

2.125000 2.750000 2.437500 -1.058594

2.437500 2.750000 2.593750 -0.272461

2.593750 2.750000 2.671875 0.138916

2.593750 2.671875 2.632812 -0.068298

2.632812 2.671875 2.652344 0.034927

2.632812 2.652344 2.642578 -0.016781

2.642578 2.652344 2.647461 0.009049

2.642578 2.647461 2.645020 -0.003878

2.645020 2.647461 2.646240 0.002584

Root is: 2.645630

Problem#5
Answer

You might also like