필터 지우기
필터 지우기

How to do a loop for different values?

조회 수: 5 (최근 30일)
Omar B.
Omar B. 2023년 11월 6일
댓글: Omar B. 2023년 11월 6일
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
Dyuman Joshi
Dyuman Joshi 2023년 11월 6일
Do you just want the plots as an output? or any other value/variable as well?
Omar B.
Omar B. 2023년 11월 6일
yes, I want to find different images for different values of q (q=0:0.2:0.99) when I run the code

댓글을 달려면 로그인하십시오.

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 11월 6일
The output images are not big/clear enough with subplot/tiledlayout.
So, this code generates a new figure for each image -
q = 0:0.2:0.99;
%Call the function
arrayfun(@myFun, q)
function myFun(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
nit = 100; % Maximum number of Newton iterations to be done
for i = 1:nit
%% Function evaluations are costly, especially for an array with 1000x1000 elements
%% Calculate once and store the data in an array instead of calculating
%% twice separately
Z0 = f(Z);
Z = Z.*(1 - (Z0.*(1-q))./(Z0-f(q.*Z)));
end
%% eps is a built-in function, better to not use it as a variable name
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);
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);
%plotting
Z = Z1 + 2*Z2 + 3*Z3 + 4*Z4 + 5*Z5;
figure
image([xmin xmax],[ymin ymax], Z);
set(gca,'YDir','normal');
axis tight; axis equal;
xlabel('$x$','Interpreter','latex','FontSize',12)
ylabel('$y$','Interpreter','latex','FontSize',12)
title(sprintf('Basins of attraction for the root of $f(x)=z^4 - 5z^2+4$ for q = %g', q),'Interpreter','latex','FontSize',12)
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by