Advertisement
Guest User

Untitled

a guest
Jul 14th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.12 KB | None | 0 0
  1. format long;
  2. es = (.5 * (10 ^ -3));
  3. f = @(z) z ^ 3 - 4 * z + 1;
  4. x0 = 0;
  5. x1 = 1;
  6. x(1) = x0;
  7. for i = 2:1:10
  8.     %ans0 = f(x0);
  9.     ans0 = (x0 ^ 3) - (4 * x0) + 1;
  10.     ans1 = (x1 ^ 3) - (4 * x1) + 1;
  11.     x(i) = x0 - ((ans0 * (x1 - x0)) / (ans1 - ans0));
  12.     ans2 = (x(i) ^ 3) - (4 * x(i)) + 1;
  13.     if(ans0 > 0 && ans2 < 0) || (ans0 < 0 && ans2 > 0)
  14.         x1 = x(i);
  15.     end
  16.     if(ans1 > 0 && ans2 < 0) || (ans1 < 0 && ans2 > 0)
  17.         x0 = x(i);
  18.     end
  19.     ea = abs(((x(i) - x(i - 1)) * 100 )/ x(i));
  20.     %disp(ea);
  21.     arr(i - 1) = ea;
  22.     fprintf('%.5f %.5f %.5f --> %.5f\n', ans0, ans1, ans2, x(i));
  23.     if(abs(ea) < abs(es))
  24.         break;
  25.     end
  26.     %plot(x);
  27.     plot(arr);
  28. end
  29.  
  30.  
  31.  
  32. --------------------------------------------------------------------------
  33.  
  34.  
  35. format long;
  36. es = .5 * (10^-1);
  37. current = 1;
  38. fprintf('%d %d\n', 1, 1);
  39. for i = 1:1:100
  40.     prev = current;
  41.     current = current + ((.5 ^ i) / factorial(i));
  42.     ea = ((current - prev) * 100) / current;
  43.     fprintf('%d %f %.4f\n', i + 1, current, ea);
  44.     %if(abs(ea) < abs(es))
  45.     if(ea < es)
  46.         break;
  47.     end
  48.    
  49. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement