Answered step by step
Verified Expert Solution
Question
1 Approved Answer
prob1Asg0.m below Problem 1 (10 points): Measuring the efficiency of several methods for creating and computing with vectors in Matlab. See prob1Asg0.m. We will take
prob1Asg0.m below
Problem 1 (10 points): Measuring the efficiency of several methods for creating and computing with vectors in Matlab. See prob1Asg0.m. We will take a given vector x, cube every value of x, and store in a new vector y. Method 1 (Vectorized Exponentiation): y = x.^3 Method 2 (Vectorized Exponentiation): y = x.*x. *x; Method 3 (For Loop with Pre-Allocated y): This is your job. Method 4 (For Loop with Construction of y by concatenation): See the given code (a) Complete Method 3 in the script prob1Asg0.m (see comments for hints) 5 Points PASTE CODE for Method 3 HERE. (b) Results Table. 3 Points Run the script prob1Asg0.m & write over my results in the table with your results. Method Runtimes (seconds) n=100000 N=10,000,000 n=1000 1. Vectorized Exponentiation 8.6e-5 3.-1 4.1e-3 2. Vectorized Multiplication 4.8e-6 5.2e-4 2.0e-2 1.5e-2 3. For Loop with Pre-Allocation 1.7e-4 1.2e+0 4. For Loop with Concatenation 1.1e-3 3.5e+0 N/A (c) Why is Method 4 bad? 2 points. % set size of vector n=1000; % create x-coordinates (n evenly spaced pts from -1 to 1) x=linspace(-1,1,n); % Method 1: Vectorized Exponent % "tic" command = start a timer tic; % cubes every element of x, = x.^3; y1 % "toc" stops the clock. We will store the results and print it t=toc; % print summarry fprintf(' Runtimes for n=%d = fprintf('Method 1 (Vectorized Exponent): %.1e seconds ',t); ',n); % erase y, pause for 0.1 seconds % Method 2: Vectorized Exponent tic; = X.*X.*x; y2 t=toc; fprintf('Method 2 (Vectorized Mult): %.le seconds ',t); Between the tic and the toc, write the code % Method 3 (Your Job): % necessary to compute x^3 and store in a new variable. tic; % First Task: create a "zero vectorll with the same size as "x" and store it under the name y3 % Second Task: Write a for loop (start at 1, end at n) to compute x^3 for example if your loop index were "j", you would have y3(j) x(j)^3 WHEN YOU ARE DONE: Paste the code between the tic and toc in the Word Document t=toc; fprintf('Method 3 (For loop with preallocation): %. le seconds ',t); % Method 4 if (n>100000) fprintf('Do not think of trying concatenation for big n! '); else tic; y4= []; % initialize null vector for j=1:n y4= [y4;x(j)*x(j)*x(j)]; % Concantenates y with end t=toc; fprintf('Method 4 (For Loop with Concantenation): %.le seconds ',t); endStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started