1. CHRISS T. EUGENIO
ES 84- F89W56
clc
//(NEWTON-RAPHSON METHOD)
//Given afunction f(x), evaluate f'(x) symbolically.
function y=f(x)
y = 75*%e^(-1.5*x) + 20*%e^(-0.075*x) - 15
endfunction
function y=g(x)
y = -112.5*%e^(-1.5*x) - 1.5*%e^(-0.075*x)
endfunction
//Allow user to input an initial estimate of the root
xi= input("initalestimate of the root:")
//Graph of the given function
x=[-2:0.5:6]
plot(x,f(x),'blue-')
a=gca()
a.x_location = "origin"
a.y_location = "origin"
mprintf("niteration tt xi tt f(xi) tt f`(xi) tt ea n")
//User-defined function for getting % absolute relative approximate error
function [ea]=PARAError(x, y)
ea=abs((y-x)/y)*100
endfunction
//Initialization, Stopping criterion, es (0.5% in this case)
i= 1
es= 0.5
ea= 100
mprintf("n%dttt%ft%ft%ft%fn",i-1,xi, f(xi),g(xi) , ea)
y=0
plot(xi,f(xi), '.red')
//Loop for approximating the estimate root of the function
while(ea>es)
xi_old = xi
fxi= f(xi_old)
gxi= g(xi_old)
xi= xi_old - (fxi/gxi)
xi_new = xi
ea = PARAError(xi_old,xi_new)
i=i+1
mprintf("n%dttt%ft%ft%ft%fn",i-1,xi,fxi, gxi , ea)
y=0
plot(xi_new,f(xi_new), '.red')
end
//Final estimate of the root plotted in the graph in yellow bullet
y=0
plot(xi_new,f(xi_new), '.yellow')
2. mprintf("nThe approximate time required toreduce the concentration to 15 is %fnn",xi_new)
//(SECANT METHOD)
//Allow user to input two initial guesses of the root (does not necessarily bracket the root)
x1= input("x1:")
x2= input("x2:")
//Graph of the function
x=[-2:0.5:6]
plot(x,f(x),'blue-')
a=gca()
a.x_location = "origin"
a.y_location = "origin"
mprintf("niteration tt x1 tt x2 tt f(x1) tt f(x2) tt xr tt f(xr) tt ea n"); //heading of the table
//Initialization, Stopping criterion, es (0.5% in this case)
i= 1
es= 0.5
ea= 100
//Loop for approximating the estimate root of the function
while(ea>es)
fx1 = f(x1)
fx2 = f(x2)
xr = x2 - ((fx2*(x2-x1))/(fx2-fx1))
fxr = f(xr)
ea = PARAError(xr,x2)
i=i+1
mprintf("n%dttt%ft%ft%ft%ft%ft%ft%f",i-1,x1,x2,fx1,fx2,xr, fxr, ea)
x1=x2
x2=xr
y=0
plot(xr,f(xr),'.red')
end
//Final estimate of the root plotted in the graph in yellow bullet
y=0
plot(xr,f(xr),'.yellow')
mprintf("nnThe approximate time required toreduce the concentration to 15 is %fnn",xr)