How can I create an array of fixed length?

조회 수: 97 (최근 30일)
John
John 2014년 3월 17일
편집: Stephen23 2017년 9월 9일
UPDATE 1: I fixed my resolution issue, but another problem arose.
I'm currently working on a project for examining the Mandelbrot set. My goal is to draw the Mandelbrot set, center the image on a specific location, and then zoom into it. When zooming I receive the following error code:
??? Error using ==> plus
Matrix dimensions must agree.
Error in ==> drawMandelbrot at 20
z = z.^2 + z0;
When k=32, my z0 matrix goes from being 600x600 to 599x600 preventing me from zooming further because I can't add it to z.^2, which stays a constant 600x600.
_____
function [xctr,yctr] = drawMandelbrot(xmin,xmax,ymin,ymax)
res=599/(xmax-xmin);
x = xmin : 1/res : xmax;
y = ymin : 1/res : ymax;
xctr = (xmin + xmax)/2;
yctr = (ymin + ymax)/2;
Length = length(x);
[X,Y]= meshgrid(x,y);
z0 = X + i*Y;
size(z0)
z = zeros(Length,Length);
c = zeros(Length,Length);
depth = 128;
for k = 1 : depth
z = z.^2 + z0;
c(abs(z) < 2) = k;
end
c;
image(c)
axis image
drawnow
colormap(flipud(jet(depth)))
end
______
MAIN
xmin = -2.1;
xmax = .7;
ymin = -1.4;
ymax = 1.4;
[xctr,yctr] = drawMandelbrot(xmin,xmax,ymin,ymax);
xctr_new = -.75;
yctr_new = 0.1;
dx = (xctr_new - xctr)/5;
dy = (yctr_new - yctr)/5;
for j=0:4;
xmin = xmin + dx;
xmax = xmax + dx;
ymin = ymin + dy;
ymax = ymax + dy;
[xctr,yctr] = drawMandelbrot(xmin,xmax,ymin,ymax);
end
delx = (xmax - xmin)/100;
dely = (ymax - ymin)/100;
for k=0:99;
xmin = xmin + .1*k*delx;
xmax = xmax - .1*k*delx;
ymin = ymin + .1*k*dely;
ymax = ymax - .1*k*dely;
k
[xctr,yctr] = drawMandelbrot(xmin,xmax,ymin,ymax);
end
_____
Is there a way to prevent 1 row of z0 from being truncated?
Any help is much appreciated.

채택된 답변

Jos (10584)
Jos (10584) 2014년 3월 18일
Maybe you want to pre-allocate z and c like this:
z = zeros(size(z0)) ;
c = zeros(size(z0)) ;
  댓글 수: 3
Akshay Joshi
Akshay Joshi 2017년 9월 9일
Consider the following scenario: I want my vector size to be 5 i.e. if I try to enter 7 elements in a vector, it should throw some error like "Maximum vector size is 5. You cannot insert more than 5 elements in it."
Is there some way to do so??
Stephen23
Stephen23 2017년 9월 9일
편집: Stephen23 2017년 9월 9일
@Akshay Joshi: You will have to either check the indices before allocating, or check the vector size after allocating.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by