Vous êtes sur la page 1sur 3

clear;clc;format(9);

//Function for evaluating f(x)


function fv=f(x)
fv=exp(0.5*x)-x-4;
endfunction;

//Half-Interval Method
disp('HALF-INTERVAL METHOD');
e=input('Error tolerance:');

//prompt for a correct pair of limits


xL=input('Lower Limit:');
xU=input('Upper Limit:');
while(f(xL)*f(xU)>0)
disp('The limits do not enclose the root. Enter new limits.')
xL=input('Lower Limit:');
xU=input('Upper Limit:');
end;

//iterate until an aceptable root is computed


xr=0.5*(xL+xU);
n=1;//initialize the iteration counter n
IT=[n,xL,xU,xr,f(xr)];//initialize the iteration table IT
while (abs(f(xr))>e)
if f(xL)*f(xr)<0 then
xU=xr;
else
xL=xr;
end;
xr=0.5*(xU+xL);
n=n+1;
IT=[IT;n,xL,xU,xr,f(xr)];
end;

clear;clc;format(12);

//Function for evaluating f(x)


function fv=f(x)
fv=exp(0.5*x)-x-4;
endfunction;

//Regula-Falsi Method
disp('REGULA-FALSI METHOD');
e=input('Error tolerance:');

xL=input('Lower Limit:');
xU=input('Upper Limit:');
while(f(xL)*f(xU)>0)
disp('The limits do not enclose the root. Enter new limits.')
xL=input('Lower Limit:');
xU=input('Upper Limit:');
end;

xr=xL-(f(xL)*(xL-xU))/(f(xL)-f(xU));
n=1;//initialize the iteration counter n
IT=[n,xL,xU,xr,f(xr)];//initialize the iteration table IT
while (abs(f(xr))>e)
if f(xL)*f(xr)<0 then
xU=xr;
else
xL=xr;
end;
xr=xL-(f(xL)*(xL-xU))/(f(xL)-f(xU));
n=n+1;
IT=[IT;n,xL,xU,xr,f(xr)];
end;
IT
xr

clear;clc;format(12);
format(6)
//Function for evaluating f(x)
function fv=f(x)
fv=exp(0.5*x)-x-4;
endfunction;

//Function for evaluating f'(x) using numerical differentiation


function fpv=fp(x)
dx=10^(-8);
fpv=(f(x+dx)-f(x))/dx;
endfunction;

//Newton Method
disp('NEWTON METHOD');
e=input('Error tolerance:');
xr=input('First trial root:');

nmax=100; //maximum number of iterations


n=1;//initialize the iteration counter n
IT=[n,xr,f(xr)];//initialize the iteration table IT
while (abs(f(xr))>e & n<=nmax )
xr=xr-f(xr)/fp(xr);
n=n+1;
IT=[IT;n,xr,f(xr)];
end;

if abs(f(xr))>e then
disp('No root found.Rerun the program and try a new initial trial
root');
else
//display the iteration table and computed root
IT
xr
end;

clear; clc; format(12)

function fv=f(x)
fv=exp(0.5*x)-x-4;
endfunction
function gv=g(x)
gv=(log(x+4))/0.5;
endfunction

//Fixed-Point Iterative Method


disp('FIXED-POINT ITERATIVE METHOD');
e=input('Error tolerance:');

xr=input('Initial trial root:');


nmax=100;
n=1; IT=[n,xr,f(xr)];

while abs(f(xr))>e & n<nmax


xr=g(xr);
n=n+1;
IT=[IT; n, xr, f(xr)];
end

if abs(f(xr))>e then
disp('No root found.')
disp(' Try a new initial trial root or change g(x)');
else
IT
xr
end

Vous aimerez peut-être aussi