How can I replace my values in an iteration?
Show older comments
This is a code I am using to run the modified secant open method. However, I don't understand why my values are not replacing the old ones each time I run the iteration.
function find_newX = modifiedsecant(initialGuess, pertubation, tolerance, func)
%%variables used as input input arguments:
%%initialGuess = initial guess for x root user inputs
%%pertubation = small pertubation fraction
%%tolerance = amount of error allowed to stop iterations
%%func = name of the function
%%the first step is to find the value of the func at the initial guess
%%func_initialGuess = func(initialGuess);
%%the second step is to find the value of the func at the (intial guess + perturbation*initial guess)
%%xp = initialGuess * perturbation
%%xp = initialGuess*pertubation;
%%func_initialGuess_and_xp = func(initialGuess + xp);
%%interative formula for modified secant method
%%this equation will be reiterated until the required tolerance is met
%%find_newX = (xp*func_initialGuess)/(func_initialGuess_and_xp - func_initialGuess);
%%fourth step is to calculate the relative approximate error to compare to the tolerance
%%assume first that ea = 100%
n = 0;
%%creating a while loop such it runs until the tolerance is reached
while n < 10
func_initialGuess = func(initialGuess); %%these are the formulas made in the previous comments put in the while loop
xp = initialGuess*pertubation;
func_initialGuess_and_xp = func(initialGuess + xp);
find_newX = (initialGuess - (xp*func_initialGuess))/(func_initialGuess_and_xp - func_initialGuess);
ea = abs((find_newX - initialGuess)/find_newX); %%calculates the new ea
find_newX = initialGuess; %%this is setting the find_newX value to the intialGuess variable
n = n+1;
display(initialGuess);
display(find_newX);
end
When I display the values for initialGuess, it will display the original value I assigned rather than replacing it with the find_newX value. I don't understand why.
Accepted Answer
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!