Fixed Point iteration using matlab, whats wrong with my code?

In summary, the conversation discusses using Matlab to create a program for fixed point iteration in order to find the root of an equation. The individual is struggling with implementing the while loop and correctly plugging in the modified form of the equation. They also ask for clarification on the purpose of the error variable and request a comment on the code.
  • #1
megr_ftw
71
0
Fixed Point iteration using matlab, what's wrong with my code??

Homework Statement


We are suppose to use MatLab to make a program using the fixed point iteration to find the root of an equation.
I just can't figure out what I'm doing wrong here...

I'm pretty sure a while loop is the appropriate measure to use, but how should I go about plugging in g(x) which is the modified form of the original equation..

Homework Equations





The Attempt at a Solution



%finds the approximate root using fixed point iteration
%input: inline function g(x)
%guess point x0
%max error (err_max)
%number of iterations (n)
%output: x is the approx. root
%k is the number of iterations carried out

function root= fixed_point(g,x0,n,err_max)
i=1;
x(1)=g(x0);
while i<=n
x(i+1)=g(x(i));
if abs((x(i+1)-x(i))/(x(i+1)) < err_max
disp('Converges after k iterations')
k=i;
disp('The root to the equation is')
x(i+1)
return
end
i=i+1;
end
 
Physics news on Phys.org
  • #2


What kind of error are you getting?
 
  • #3


Could you comment your code? I can't quite figure out how this is supposed to perform fixed point iteration.
 

FAQ: Fixed Point iteration using matlab, whats wrong with my code?

1. What is fixed point iteration and how is it used in MATLAB?

Fixed point iteration is a numerical method used for solving equations by repeatedly substituting a value for a variable until the equation is satisfied. In MATLAB, fixed point iteration can be implemented using a loop to update the variable and check for convergence.

2. Why is my code not converging when using fixed point iteration in MATLAB?

There could be several reasons for this issue. Some common reasons include using an incorrect initial guess, a poor choice of iteration function, or a mistake in the implementation of the algorithm. It is important to carefully check your code and make sure it follows the correct steps for fixed point iteration.

3. Is there a way to speed up the convergence of fixed point iteration in MATLAB?

Yes, there are several techniques that can be used to improve the convergence of fixed point iteration. These include using a better initial guess, choosing a different iteration function, or using a different numerical method altogether.

4. How do I know if my fixed point iteration code in MATLAB is accurate?

The accuracy of your code can be evaluated by comparing the results to known solutions or by checking for convergence. If the results are close to the known solution and the iteration process stops after a reasonable number of iterations, then your code is likely accurate.

5. Can fixed point iteration be used for all types of equations in MATLAB?

No, fixed point iteration is only suitable for certain types of equations that have a fixed point. This means that the equation must have a unique solution that can be found by repeatedly substituting a value for a variable. If the equation does not have a fixed point, then another numerical method should be used.

Similar threads

Replies
1
Views
1K
Replies
9
Views
3K
Replies
1
Views
982
Replies
7
Views
1K
Replies
1
Views
20K
Replies
3
Views
1K
Replies
6
Views
2K
Back
Top