1

I am attempting to find the root of the equation 3x3+x2-10 using iteration. Here is the code that I have so far, however I don't think I have implemented the while loop correctly since it the equation is currently only evaluated once. Essentially, the code ought to evaluate whether the absolute value of xVal-cVal is less than 0.0001 and if so, it should print out the value of cVal and stop. Otherwise, the value of xVal should be set to equal cVal and loop back, however the code does not do this.

xVal = input('Enter a value for x:');
cVal =((10-xVal^2)/3)^(1/3);
while (abs(xVal-cVal)>0.0001)
   xVal = cVal;
end
disp(cVal);

Image of flowchart for equation

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
compscistudent
  • 107
  • 1
  • 2
  • 11
  • You need to evaluate a new `cVal` inside the loop after assigning the old value to `xVal`. If Matlab has a do-while or do-until, that would be the ideal construct. – lurker Oct 27 '18 at 14:08
  • So while (abs(xVal-cVal)>0.0001) xVal = cVal; cVal =0; end – compscistudent Oct 27 '18 at 14:12
  • No, 0 isn't the correct new value for `cVal`. Think about it and don't just guess. That would just infinitely loop and test a value of 0. It needs to be computed as done the first time. – lurker Oct 27 '18 at 14:12
  • Could you provide a link to the algorithm/formula you used? – Cepheus Oct 27 '18 at 14:16
  • I don't have a link I'm afraid - I just rearranged the equation 3x^3+x^2-10 to make x the subject so x_(n+1)=∛((10-x^2)/3) – compscistudent Oct 27 '18 at 14:22
  • I doubt that this works because one of the xs remains on the right side of the equation, so you act as if they were different numbers. Consider implementing https://en.wikipedia.org/wiki/Newton%27s_method. – Cepheus Oct 27 '18 at 14:24
  • You have an equation with two x-es and you solve for one of them? That is not how root finding works. The two x-es have the same value! I would recommend you start implementing [the bisection medthod](https://en.m.wikipedia.org/wiki/Bisection_method), and look for more complex methods after. – Cris Luengo Oct 27 '18 at 14:38
  • I've added a link to an image of the flowchart that I'm trying to implement if that's of any help? – compscistudent Oct 27 '18 at 14:48
  • 1
    To implement that flow chart, you need to recompute `cVal`. But I’m not certain that it necessarily converges. Is this your algorithm or did you get it from somewhere? – Cris Luengo Oct 27 '18 at 16:35
  • 2
    I will confirm that this fixed-point iteration converges if implemented correctly (i.e., `cVal` is updated within the `while` loop. – TroyHaskin Oct 27 '18 at 23:42

2 Answers2

2

The methodology in the provided flowchart image will not work.

Try this script:

func = @(xVal)((10-xVal^2)/3)^(1/3);
x = fzero(func,StratingX)
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
1

You need to update cVal in your while loop.

xVal = input('Enter a value for x:');
cVal =((10-xVal^2)/3)^(1/3);
while (abs(xVal-cVal)>0.0001)
   xVal = cVal;
   cVal =((10-xVal^2)/3)^(1/3);
end 
disp(cVal);

Output:

Enter a value for x:0
    1.3905
Banghua Zhao
  • 1,518
  • 1
  • 14
  • 23