MATLAB Answers

How can I iterate all three cases at once in one script?

1 view (last 30 days)
Hi!
I'm taking an applied computational methods class next semester, and I don't have significant expierence with using MATLAB, so I'm taking my winter break to get familiar with some things.
One of the things I'm working on right now is how to implement the fixed point iteration method to find the roots of non-linear equations in MATLAB. I have a code that works, but there are multiple cases where one can rewrite an equation in a manner , so I would like to figure out a way to compute all three cases at once, rather than having to change every single time and in multiple lines.
For example: consider the nonlinear equation . Algebraically, we know that the roots are , but I would like to find them numerically via simple fixed-point iteration.
Rewriting we get three cases
Where Case 1 monotonically converges to 3, case 2 converges in an ossilatory fashion to -1, and case 3 diverges.
I've included my code below.
Also one more thing... I tried changeing x1 = 4; to x0 = 4 so that x1 = g1(x0), but I had issues with trying to iterate properly. Is there anyway I could do that? x1 as the initial root guess confuses me, but I can live with it.
Thank you!
close all, clear all, clc;
format long;
%# You function here
g1=@(x) sqrt(2*x + 3); % Case 1 (converges monotonically)
% g2=@(x) 3/(x-2); % Case 2 (converges oscillatory)
% g3=@(x) (x^2 -3)/(2); %Case 3 (Iterates diverge)
%# Start out iteration loop
x1 = 4;
x2 = g1(x1);
iterations = 0;% # iteration counter
% ezplot(g1,[0,1]);
% hold on
% ezplot('x',[[0,1]])
while (abs(x2-x1) > 1e-5 && iterations<100)
% plot([x1 x1], [x1 x2], 'k-')
% plot([x1 x2], [x2 x2], 'k--')
%pause
iterations = iterations + 1;
x1 = x2;
x2 = g1(x1);
end
iterations
[x1 x2]

  0 Comments

Sign in to comment.

Accepted Answer

Chuguang Pan
Chuguang Pan on 20 Dec 2019
Maybe you can use a for loop. You can store functions g1(x), g2(x), g3(x) as a cell array. Such as
g1=@(x) sqrt(2*x+3);
g2=@(x) 3./(x-2);
g3=@(x) (x.^2-3)/2;
h={g1;g2;g3}; %h is a cell array.
for i=1:3
G=h{i}; %G is the function handle which you want to use
%Do iteration loop
end

  4 Comments

Show 1 older comment
Chuguang Pan
Chuguang Pan on 20 Dec 2019
close all, clear all, clc;
format long;
%# You function here
g1=@(x) sqrt(2*x + 3); % Case 1 (converges monotonically)
g2=@(x) 3/(x-2); % Case 2 (converges oscillatory)
g3=@(x) (x^2 -3)/(2); %Case 3 (Iterates diverge)
h = {g1; g2; g3};
X1=zeros(3,1); %Storing every x1 result
X2=zeros(3,1); %Storing every x2 result
for i = 1:3
G = h{i};
x1 = 4;
x2 = G(x1);
iterations = 0; % Do something to this?
while (abs(x2-x1) > 1e-5 && iterations<100)
iterations = iterations + 1; % Do something to this?
x1 = x2;
x2 = G(x1);
end
X1(i)=x1;%storing x1
X2(i)=x2;%storing x2
end
% iterations % Do something to this?
% [x1,x2]
Nathan Nguyen
Nathan Nguyen on 20 Dec 2019
also, how can I find out how many iterations each equation takes ?

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 20 Dec 2019
"rather than having to change to and every single time and in multiple lines."
The problem is that you've hamstrung yourself from the get go because you've embedded an index into your variable names. Numbered variables are always a bad idea.
Matlab already has a very simple way of indexing things, the index that you get with matrices, cell arrays, or other containers. In your case, since you're storing function handles you should use a cell array:
g{1} = @(x) sqrt(2*x + 3); % Case 1 (converges monotonically)
g{2} = @(x) 3/(x-2); % Case 2 (converges oscillatory)
g{3} = @(x) (x^2 -3)/(2); %Case 3 (Iterates diverge)
you can then have a variable funindex that you change appropriately depending on which function you want to use
funindex = randi(3); %obviously your funindex is determined some other way
result = g{funindex}(rand); %and apply the correct function to whatever
This is more or less what chuguang did in a bit more roundabount way. You shouldn't create g1, g2, and g3 in the first place

  0 Comments

Sign in to comment.

Sign in to answer this question.