Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLAB已退休,不会更新。

Rooting Around in MATLAB – Part 3

I recently wrote a pair of posts (1and2) about finding roots of equations, and how you might use MATLAB to explore this topic with emphasis on the method offixed point iteration

Contents

Set Up

清除allcloseall

例子提醒

Let me restate the example function. Let's start with a simple cubic polynomialF(x)

which we define using an anonymous function.

f = @(x) x.^3 + x - 1; fplot(f,[-2 2]) titlef网格on

First Fixed Point Iteration Attempt

Previously we rewroteF(x)=0以便x是阿尔one on one side of the equation.

g1 = @(x) 1 - x.^3;

This function,G1(x)did not help find the root between 0 and 1 - every step took us further away from the solutions we found withrootsandfzero

fzsolution = fzero(f,0.5)
fzsolution = 0.68233

重写方程的第二种方法

还有另一种方法可以为固定点编写方程式。记住,我们想重新排列F(x)=0像这样的东西g2(x)=x。我们已经尝试了g1 = 1 - x^3。我们还可以将方程式重写为

g2 = @(x) 1./(x.^2+1); fplot(g2,[0 1]); holdonstraightLine = @(x) x; fplot(straightLine, [0 1],'G') 传奇('g2','x','Location','SouthEast') 网格on平等的, axis([0 1 0 1])

让我们迭代

跟随ing the same idea from the last post, we now iterate, finding successive guesses ofx, computing the value ofg2(x), setting this value to be the next estimate ofx, etc.

让我们尝试在解决方案上“零”

首先,我们选择一个起始值x = 0.5, andy,从直线也在0.5处。

x(1)= 0.5;y(1)= x(1);x(2)= x(1);y(2)= g2(x(2));

让我们绘制解决方案的轨迹,从第一点开始。金宝搏官方网站

情节(x,y,'r',x(1),y(1),,'r*')

首次迭代

如上一篇文章所述,我现在设置了当前值g2到新的xvalue, sliding onto the straight line, and then calculate the next value ofg2从这个新价值x。并绘制它。

x(3) = y(2); y(3) = y(2); x(4) = x(3); y(4) = g2(x(4)); plot(x,y,'r')

Second Iteration

这是第二个迭代。

n = 5;x(n)= y(n-1);y(n)= y(n-1);x(n+1)= x(n);y(n+1)= g2(x(n+1));情节(x,y,'r')

第三次迭代

And the third.

n = 7; x(n) = y(n-1); y(n) = y(n-1); x(n+1) = x(n); y(n+1) = g2(x(n+1)); plot(x,y,'r')

再回合

让我们再做几次迭代,然后对阵列进行预先分配,而不是种植它们。是的,我知道在这篇文章中,我不断地延长了线条。我不希望我们通过管理这些地块而分心。

x(9:22)= nan;y(9:22)= nan;forn = 9:2:21 x(n)= y(n-1);y(n)= y(n-1);x(n+1)= x(n);y(n+1)= g2(x(n+1));end情节(x,y,'r') holdoff

Fixed Point Found!

We appear to be converging on a fixed point, since after iterating, the final values forxandg2(x)(which isy)彼此非常接近。

final = [x(end)y(end)fzsolution]
最终= 0.68039 0.68356 0.68233

Wrapping Up the Series of Posts

This post completes this series of posts on fixed point iteration. There is, of course, more that you could do in a class. For example, you might explore what characteristics of the functionsg1andg2使固定点迭代策略的行为不同。也许看第一衍生物?

这是该系列的回顾

Is this sort of tutorial relevant to your work, especially if you are teaching? How about the incorporation of the visualization during the exploration? Let me know your thoughts这里




Published with MATLAB® 7.8

|