0% found this document useful (0 votes)
28 views5 pages

Make A Script File of Newton's Method That Calculates The Derivative by Itself.

The document describes a script file that calculates the root of a function using Newton's method. The script takes a function, its derivative and initial guesses as inputs and iteratively finds the root. It outputs the values at each iteration until convergence within a set number of iterations.

Uploaded by

Rayhan Saeed
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)
28 views5 pages

Make A Script File of Newton's Method That Calculates The Derivative by Itself.

The document describes a script file that calculates the root of a function using Newton's method. The script takes a function, its derivative and initial guesses as inputs and iteratively finds the root. It outputs the values at each iteration until convergence within a set number of iterations.

Uploaded by

Rayhan Saeed
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/ 5

LAB

Assignment # 02

Name: Muhammad Reehan Saeed


Reg # FA18-BME-041
Class: BME-5B
To: Miss Sharmeem Shahid
Dated: 22 – 11 – 2020
Day: Sunday
WAH
CAMPUS
Question # Make a script file of Newton’s Method that
calculates the derivative by itself.

Program:
function [x]=newton1(fun,f,df,dff,a,b,acc,N,z,z1,x0,i)
syms x;
fun=input('Enter the function as variable of x : ');
f=inline(fun);
z=diff(f(x));
df=inline(z);
z1=diff(df(x));
dff=inline(z1);
char x;
a=input('Enter Initial root Guess : ');
b=input('Enter another Initial Root Guess : ');
if (f(a)*f(b)>0)
error('Root does not lie in this region');
else if (f(a)*dff(a)>0)
x0=a;
else
x0=b;
end
end
N=input('Enter the number of itterations : ');
i=1;
x=x0;
x(x0-1)=x0-f(x0)/df(x0);
while (i<=N)
x(i+1)=x(i)-f(x(i))/df(x(i));
i=i+1;
end
end

Output :
>> newton1

Enter the function as variable of x : x^3-x-1

Enter Initial root Guess : 1

Enter another Initial Root Guess : 2

Enter the number of itterations : 6

ans =

1.5455 1.3596 1.3258 1.3247 1.3247 1.3247

You might also like