Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Read Chapter 13 of the Moler book. You can also read Chapter 6 to get an introduction to a couple of the MATLAB functions that

Read Chapter 13 of the Moler book. You can also read Chapter 6 to get an introduction to a couple of the MATLAB functions that we are using such as tic and toc. The file assignment3.m provides you with template code that makes a medium-resolution video that pans across a Mandelbrot image. As explained in Chapter 13, the mandelbrot_step function allows for compiled C language to be executed instead of MATLAB code and this can allow for the implementation to be optimized. Look at the mandelbrot_step.c file and compare with the mandelbrot_step.m file. What do you think is the primary optimization that file mandelbrot_step.c leverages? [5 MARKS]

Through use of the variable DO_IN_PARALLEL, we can use parfor instead of for to iterate through the frames. The code as provided uses parfor. Record again how long it takes the code as provided to calculate all the frames. ________ Now set DO_IN_PARALLEL to false to switch to using for. Now how long does it take to calculate all the frames? _________ At this point, I suggest switching back to having DO_IN_PARALLEL set to true. [5 MARKS]

MATLAB usually come with lots of functions about precision. Display the Mandelbrot set with the fixed point, floating point and double floating point precision; and create a table for execution time with different precisions. You also will need to report execution times with and without the optimizations for examples you provide, and a description of the circumstances under which the optimizations should be useful. [20 MARKS]

The goal of this assignment is to create a beautiful video that does a combination of zooming and panning into (and/or out of) the Mandelbrot image. Instead of panning from 0.5+0i to -1.5+0i, start by modifying the program as provided to make a nice zoom into the image from point -1.5+0i. You can make sensible improvements to the template code to facilitate this and the further requirements below. How far into the image can you zoom before the resulting image frame gets grainy and pixelated? Why does this happen? Can you use the variable precision in MATLAB to solve this problem? You can use the "vpn" function in MATLAB, for 64bit precision results. [50 MARKS]

While we hope your video will have artistic merit, you can also get credit for using interesting math while creating your video. Use your imagination. Make sure you highlight the artistic and mathematical merit of your video/programming before you submit your work. You can use whatever aspect ratio you want. While you are doing exploratory work, feel free to reduce resolution and frame rate, etc. Once you have a better idea of your starting point and your path of panning and zooming, etc., increase the resolution, and perhaps fame rate and depth parameters in order to bring your video to a quality that you are happy with. Feel free to use university computers, at least while finalizing your video, if your computer is slower. The depth parameter does not need to be constant during calculations for each frame that you calculate. [20 MARKS]

You will need to upload your file assignment3.m as well as your video file. As an alternative bonus question, try to explain exactly why mandelbrot_step.c produces slightly different images than mandelbrot_step.m, at least on 64-bit Microsoft Windows computers where I have tested this. Prove your explanation. OR Demonstrate the simulation speedup using onboard GPU (if your computer has one !).

function frameArray = assignment3

MAX_FRAMES = 256; % you can change this and consider increasing it.

RESOLUTION = 512; % you can change this and consider increasing it.

FRAMERATE = 30; % you can change this if you want.

WRITE_VIDEO_TO_FILE = false; % change this as you like (true/false)

DO_IN_PARALLEL = true; %change this as you like (true/false)

if DO_IN_PARALLEL

startClusterIfNeeded

end

if WRITE_VIDEO_TO_FILE

openVideoFile

end

% Colors

depth = 32; % you will probably need to increase this, maybe dynamically.

CMAP=flipud(jet(depth)); %change the colormap as you want.

%preallocate struct array

%frameArray=struct('cdata',cell(1,MAX_FRAMES),'colormap',cell(1,MAX_FRAMES));

DISTANCE = 2; % total panning distance

STEP = DISTANCE/MAX_FRAMES; %how much to pan per step.

iterateHandle = @iterate;

tic % begin timing

if DO_IN_PARALLEL

parfor frameNum = 1:MAX_FRAMES

%evaluate function iterate with handle iterateHandle

frameArray(frameNum) = feval(iterateHandle, frameNum);

end

else

for frameNum = 1:MAX_FRAMES

if WRITE_VIDEO_TO_FILE

%frame has already been written in this case

iterate(frameNum);

else

frameArray(frameNum) = iterate(frameNum);

end

end

end

if WRITE_VIDEO_TO_FILE

if DO_IN_PARALLEL

writeVideo(vidObj, frameArray);

%movie2avi(frameArray,'assignment3m2a'); % deprecated

end

close(vidObj);

toc %end timing

else

toc %end timing

%clf;

shg; % bring the figure to the top to be seen.

movie(frameArray,1,FRAMERATE);

end

function startClusterIfNeeded

myCluster = parcluster('local');

if ~length(myCluster.Jobs) | ~strcmp(myCluster.Jobs.State, 'running')

PHYSICAL_CORES = feature('numCores');

%PHYSICAL_CORES = 4; %valid for the i7 on my desktop

LOGICAL_PER_PHYSICAL = 2; % "hyperthreads" per physical core

% you can change the NUM_WORKERS calculation below if you want.

NUM_WORKERS = (LOGICAL_PER_PHYSICAL + 1) * PHYSICAL_CORES

myCluster.NumWorkers = NUM_WORKERS;

saveProfile(myCluster);

disp('This may take a couple minutes when needed!')

tic

parpool(NUM_WORKERS);

toc

end

end

function openVideoFile

% create video object

vidObj = VideoWriter('assignment3');

%vidObj.Quality = 100; % or consider changing

vidObj.FrameRate = FRAMERATE;

open(vidObj);

end

function frame = iterate (frameNum)

% you will need to change the next set of lines for sure.

centreX = 0.5 - (frameNum-1) * STEP;

centreY = 0;

zoom = 1.5;

x = linspace(centreX - zoom, centreX + zoom, RESOLUTION);

%you can modify the aspect ratio if you want.

y = linspace(centreY - zoom, centreY + zoom, RESOLUTION);

% the below might work okay but you can further optimize it.

% Create the two-dimensional complex grid using meshgrid

[X,Y] = meshgrid(x,y);

z0 = X + i*Y;

% Initialize the iterates and counts arrays.

z = z0;

z(1,1) = z0(1,1); % needed for mex, assumedly to make z elements separate

%in memory from z0 elements.

% make c of type uint16 (unsigned 16-bit integer)

c = zeros(RESOLUTION, RESOLUTION, 'uint16');

% Here is the Mandelbrot iteration.

c(abs(z) < 2) = 1;

%don't show warning from mex invocation.

WarningOff

for k = 2:depth

[z,c] = mandelbrot_step(z,c,z0,k);

% mandelbrot_step is a c-mex file that does one step of:

% z = z.^2 + z0;

% c(abs(z) < 2) = k;

end

% create an image from c and then convert to frame. Use cmap

frame = im2frame(ind2rgb(c, CMAP));

if WRITE_VIDEO_TO_FILE & ~DO_IN_PARALLEL

writeVideo(vidObj, frame);

end

disp(['frame=' num2str(frameNum)]);

end

end

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

LO3 Define job design and identify common approaches to job design.

Answered: 1 week ago