Question
In MATLAB Easy Problem 1 Vectorize the following loops: Part A for i = 1:5 vec1(i) = (5*i+2)^3; end vec1 Part B i = 1;
In MATLAB
Easy Problem 1
Vectorize the following loops:
Part A
for i = 1:5
vec1(i) = (5*i+2)^3;
end
vec1
Part B
i = 1;
while i<=5
vec2(i) = i^2+4;
i = i+1;
end
vec2
Part C
Can you vectorize the following? Why?
vec3(1) = 1;
i = 1;
while i<5
i = i+1;
vec3(i) = (vec3(i-1)+2)^2;
end
vec3
Part D
You can vectorize the following:
for i = 1:10
a = i^2+1;
if a>10
vec4(i) = a;
else
vec4(i) = 0;
end
end
vec4
Part E
What is the inner (scalar) and outer (matrix) products of the following two vectors?
vec5 = (1:5);
vec6 = (2:6);
Ramp up Problem
Part A
We are given the function $f(x,y) = x^2 + y^2 + xy$. Write an inline function that accepts vectors of x and y to generate the value of the function. Output the answer for |x = -2:2| and |y = -1:3|.
Part B
Use a scatter plot |scatter3(arg1,arg2,arg3)| to visualize your answer now using |x=-2:0.1:2| and |y=-2:0.1:2|.
Part C
Plot this function (2D surface) between x = (-2,2) and y = (-2,2) using the |scatter3()| function. You will need to generate all permutation of (x,y) points as follows:
x = [1 2]; y = [2 3];
xp = [1 2 1 2];
yp = [2 2 3 3];
You will need to write a code that generates these |xp| and |yp| for given x and y vectors of equal length.
Touchdown Problem 3
Q3.
Convert the following for loops into vectorized forms
clear x
variable x is used above, see what happens if you don't do so
Part A
cumsum = 0;
not specifying this throws an error
for i = 1:2:10
cumsum = cumsum + i;
end
Part B
j = 1;
for i = 1:2:6
x(j) = 2+4*(i-1)^2;
j = j+1;
Part C
tstart=0; tend=20; ni=8;
t(1) = tstart;
y(1)=12 + 6*cos(2*pi*t(1)/(tend-tstart));
for i=2:ni+1
t(i) = t(i-1)+(tend-tstart)/ni;
y(i)=12 + 6*cos(2*pi*t(i)/(tend-tstart));
end
Part D
What is the problem with the following piece of code? Will it work for |x = 2|? What can you do to rectify it?
x = 3;if x>2
y = 1
else if x<2
y = 0
end
Step 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