1

I have a equation f(x)=exp(x)+3x^2, f(x)=0, x=? then I use scilab to solve that equation using fixed point iteration this is my code

function fixed_point(fung,x0,err)
x=zeros(100);
ea = 100;
i = 1;
x(i)=x0;
printf(" \t i \t x(i) \t       ea(%%)");
printf("\n\t %d \t %f \t %f", i, x(i), abs(ea));
while (abs(ea) >err) do
    i=i+1;
    z =x(i-1);
    x(i) = evstr(fung)+z;
    ea =100*(x(i)-x(i-1))/x(i);
    printf("\n\t %d \t %f \t %f", i, x(i), abs(ea));
end

printf("\n Akar = %f", x(i));
endfunction

then I call it using:

fixed_point ('exp(z)-(3 .* z .*z)',0.00000000001,0.5)

I got x(i)=inf at last, but I think that's not the answer, can someone explain to me what's wrong with my code?

duplode
  • 31,361
  • 7
  • 69
  • 130
Zahi Azmi
  • 11
  • 5
  • I don't understand the method you are trying to implement. Specially here: `x(i) = evstr(fung)+z;` Here you are calculating `f(x(i-1))+x(i-1)`, could you explain this method or link the algorithm? – Daniel Feb 27 '15 at 11:57
  • There are no real solutions: http://www.wolframalpha.com/input/?i=f%28x%29%3Dexp%28x%29%2B3x%5E2 – Daniel Feb 27 '15 at 12:02
  • thanks for your comment, i use fixed point iteration, https://mat.iitm.ac.in/home/sryedida/public_html/caimna/transcendental/iteration%20methods/fixed-point/iteration.html , http://en.wikipedia.org/wiki/Fixed-point_iteration, function x(i)=evst... means x(i)=f(z)+z or x(i)=f(x(i-1))+x(i-1) @Daniel – Zahi Azmi Feb 27 '15 at 12:07

1 Answers1

1

Let's divide the answer to "subproblems":

  1. In general: don't use numerical methods if you don't have an idea of solution. As Daniel showed, this equation doesn't have any solution in reals. If you have a reasonable x0, plot its neighborhood first!

  2. In general: is your goal solving the equation or implementing that method? Matlab has e.g. fsolve function (and you've added matlab tag as well as scilab) or, if you want to design your own function, the Newton's method works pretty well on these easily differentiable functions (sin, exp, x^n...).

  3. In particular: add to your code something that allow you to escape the while loop if the solution doesn't converge. Than your output should be "DOESN'T CONVERGE" which is definitely better than the mysterious inf (e.g. break the loop if i>1e3 or something like that).

Victor Pira
  • 1,022
  • 1
  • 10
  • 27