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

Yuki X Yuki X X X: Disp Disp Disp

This program uses the secant method to solve a function. It prompts the user to input initial values for x1 and x2, as well as the maximum number of iterations and error tolerance. It then iteratively calculates new x values using the secant formula until the error is below the tolerance or the maximum iterations is reached, displaying the results. Upon completion, it asks if the user wants to solve again with different initial values and tolerance.

Uploaded by

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

Yuki X Yuki X X X: Disp Disp Disp

This program uses the secant method to solve a function. It prompts the user to input initial values for x1 and x2, as well as the maximum number of iterations and error tolerance. It then iteratively calculates new x values using the secant formula until the error is below the tolerance or the maximum iterations is reached, displaying the results. Upon completion, it asks if the user wants to solve again with different initial values and tolerance.

Uploaded by

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

Name: Yuki D.

Sakakibara BS EE

ES 84 SecantMethod

Section: S789M89

Program for the problem using secant method:


function yuki=g(x)
//defining my function to be called later
yuki=(1/x^3)-(2/(x^2))+(1/x)-(2/3)
// letting epsilon to be equal to x
endfunction
i=1;
disp ("This program is to solve a function using secant method")
//Intro by yuki
disp("++++++++++++++++++++Created by Yuki+++++++++++++++++++++")
disp("Please input the two initial values of x:")
while i==1
//this while loop enables the user to decide wither to try the program again or not
xz=input("xz="); //inputing x-1 value
x0=input("x0=");
//inputing x0 value
disp("Please input the imax and es:")
imax=input("imax=");
//inputing the imax value
es=input("es="); //inputing the stopping criterion
iter=0;
//setting iter=0
ea=1000;
//setting ea to 1000 to ensure that the while loop will be true
xold=xz;
//setting xold equal to xz
xr=x0;
//setting xr equal to x0
x=0;
//x variable to be used to store xr
disp("=========================================================
==================")//use to make table
disp("
number of iter
xold
x
xr
ea(%)")
disp("=========================================================
==================")
while (iter<imax&ea>es)// looping the process while iter is less than imax AND stopping criterion ea
is greater than es
fxold=g(xold);//calculating function fxold when xold is substituted to the function y=g(x) the above
function
fxr=g(xr); // calculating the function fxr when xr is substituted to the function y=g(x) in the above
function
x=xr;// storing xr to variable x so that we can use it in the next iter
xr= xr-((fxr)*(xold-xr))/(fxold-fxr); //secant formula in getting xr or epsilon value
ea=abs((xr-x)/xr)*100;// getting the iterative relative error
iter=iter+1; //hence the first iteration is finish, we increment iter by 1
printf(" %10.4f
%10.4f
%10.4f
%10.4f
%10.4f",iter,xold,x,xr,ea)
//print f command is for displaying the answer for each iteration at 4 decimal places
printf("\n")// so that after the answer is printed this command will move the cursor down to the
next line
xold=x;// we now set the variable that we store in x to be xold which would be used in the next
iteration
end
disp("=========================================================
==================")
disp("+++++++++++++++++++++++YUKI@SAKAKIBARA@ES84+++++++++++++++++++
+++++++++++++")//ending statement by yuki
disp("Do you like to solve it back using another initial values and es?")//asking a question wither you
like to continue solving
w=input("1(Yes) or 0(No)?");
// if yes w=1 or if no then w=0
if w==1 then
// condition if w==1, that is, we want to continue solving
i=1;
//i is still 1 and the first while loop above is still true!
else if w==0 then
//we want to stop
i=i+1;
// i is now 2 and the first while loop above is now False and therefore the
program ends!
disp("-----------------------Thank You!------------------------------------------")
//ending statement by
yuki

end
end
end
//note that if the initial values given causes a divergence of the function an error might occur that
//is why we need to set our initial values that will not cause our function to diverge

Simulation Output Screenshots :

I f w e c h o o s e Yes :

If we choose No:

You might also like