How to do a loop for different values?
Show older comments
I am trying to find basin of attraction of the roots, and I want to run the code for different values of q ( where q=0:0.2:,0.99) . How to get the results for all the values of q ?
f = @(z) z.^4 - 5*z.^2+4; % Function with real roots
% the roots
r1 =-2;
r2 =-1;
r3 =1;
r4=2;
%% range of basin
nx = 1000;
ny = 1000;
xmin = -4; xmax = 4;
ymin = -2; ymax = 2;
x = linspace(xmin,xmax,nx); % Real space
y = linspace(ymin,ymax,ny); % Imaginary space
[X, Y] = meshgrid(x,y);
Z = X + 1i*Y;
%% Perform our method
q=0.95;
nit = 100; % Maximum number of Newton iterations to be done
for i = 1:nit
Z = Z - (f(Z).*Z.*(1-q))./ (f(Z)-f(q.*Z));
end
%% plotting
eps = 1e-10; % Tolerance to determine closeness to 0.0
Z1 = abs(Z - r1) < eps; Z2 = abs(Z - r2) < eps;
Z3 = abs(Z - r3) < eps; Z4 = abs(Z - r4) < eps;
Z5 = ~(Z1 + Z2 + Z3+Z4);
figure;
map = [0.4660 0.6740 0.1880; 0.8500 0.3250 0.0980; 0 0.4470 0.7410; 0.9290 0.6940 0.1250; 0 0 0];
colormap(map);
Z = Z1 + 2*Z2 + 3*Z3 + 4*Z4 + 5*Z5;
image([xmin xmax],[ymin ymax], Z);
set(gca,'YDir','normal');
axis equal; axis tight;
xlabel('$x$','Interpreter','latex','FontSize',14)
ylabel('$y$','Interpreter','latex','FontSize',14)
title('Basins of attraction for the root of $f(x)=z^4 - 5z^2+4$.','Interpreter','latex','FontSize',14)
4 Comments
Dyuman Joshi
on 6 Nov 2023
Edited: Dyuman Joshi
on 6 Nov 2023
Convert the script to a function that accepts "q" as an input and call it in a loop.
Omar B.
on 6 Nov 2023
Dyuman Joshi
on 6 Nov 2023
Do you just want the plots as an output? or any other value/variable as well?
Omar B.
on 6 Nov 2023
Accepted Answer
More Answers (0)
Categories
Find more on Modify Image Colors in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



