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

14 次查看(过去 30 天)
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]

采纳的回答

Chuguang Pan
Chuguang Pan 2019-12-20
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 个评论
Nathan Nguyen
Nathan Nguyen 2019-12-20
also, how can I find out how many iterations each equation takes ?

请先登录,再进行评论。

更多回答(1 个)

Guillaume
Guillaume 2019-12-20
"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

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by