MatLAB | Mathematica | MathCAD | Maple Answers

Questions: 245

Answers by our Experts: 142

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Search & Filtering

In this part, you will use logical vectors to make multiple decisions in parallel concerning values in two arrays. Firstly, generate two random [1x10] arrays using randi. The numbers should be integers between 0 and 100. Let’s call these two arrays a1 and a2.
Your task is to generate a third array, a3, which at each index, contains the largest number out of the two earlier arrays. You must accomplish this using a logical vector and no loops.

An example snippet of code that would accomplish the same task, with a for-loop, is given below:

% For each index i, the corresponding a3
% value will be the larger of a1(i), a2(i)
for i=1:10
a3(i) = max(a1(i),a2(i));
end

Hint: try constructing a logical vector of size [1x10] to store logical values based on if a1(i) > a2(i)
Create three scripts with the program code from the third example (3-D array) in Part 3.

 In one script, copy-and-paste the nested for-loop 3-D array program as given below.

 In the second script, add the array pre-allocation version of the program

 In the third script, insert your vectorised version of the program

Now, experiment with increasing the value of n in the original nested for-loop program, until the execution time takes over 3 seconds to run. To do this, either use the “Run and Time” button on the Editor tab – or use the tic and toc commands. Now run the equivalent second and third scripts (for the same value of n). How much faster is the vectorised solution compared with the original program code?

% 3-D array where
% C(i,j,k) = i*j+k
n = 10;
for i=1:n
for j=1:n
for k=1:n
C(i,j,k) = i*j+k;
end
end
end
Now, for each of the for-loop examples on the next page, write two programs that generate the same arrays (1-D, 2-D and 3-D), with the following changes:
1. Modify the programs to pre-allocate the arrays first
2. Use vectorisation functions to implement the programs without the use of any loops. You may want to
look at ndgrid, and remember to use array versions of your operators!

% 1-D array (3 * i)
n = 10;
for i=1:n
A(i) = 3*i;
end

% 2-D array where
% B(i,j) = i/j
n = 10;
for i=1:n
for j=1:n
B(i,j) = i/j;
end
end

% 3-D array where
% C(i,j,k) = i*j+k
n = 10;
for i=1:n
for j=1:n
for k=1:n
C(i,j,k) = i*j+k;
end
end
end
Create a nested for-loop script to iterate through all values of i=1:20 and j=1:20. Also start with a
running tally of total=0.
At each iteration, check the value of j. If j is divisible by 3, then do nothing and proceed to the next value of j. Next, if i x j is greater than 100, then stop processing the current j-based for-loop, and move on to the next value of i.
If both conditions have not been met, then add both i and j to the running tally called total. What is your final value for total at the end of your script? And how many times did MATLAB execute the break and continue statements in your program?
Write MATLAB programs to find the following sums with for loops and by vectorization. Time both versions in each case.
1^2 + 2^2 + 3^2 +· · ·+ 1000^2 (sum is 333,833,500).

1 − 1/3 + 1/5 - 1/7 + 1/9 -· · ·- 1/1003 (sum is 0.7849—converges slowly to π/4).

Sum the left-hand side of the series:
(1/1^2.3^2) + (1/3^2.5^2) + (1/5^2.7^2) +· · ·=(pi^2 - 8)/16 (sum is 0. 1169 − with 500 terms).
Tic-Tac-Toe (a.k.a. Noughts and crosses) is a game played by two players (‘X’ and ‘O’) on a 3x3 grid. In MATLAB, use a 3x3 cell array to hold the board information. Take it in turns to play the game, inserting ‘X’s and ‘O’s at various spots. Implement the following structure plan in MATLAB.

1) Commence the game, display empty board
2) Select which player is ‘X’ and ‘O’ (at random)
3) Request player1’s move
4) Input Player1’s move. Redraw updated board.
5) Decision: has the game been won yet? If so, go to step 9). If not, go to step 6).
6) Request player2’s move
7) Input Player2’s move. Redraw updated board.
8) Decision: has the game been won yet? If so, go to step 9). If not, go back to step 3).
9) Display winner on the screen.
Create a structure array of at least 10 imaginary students in the class – choose names, numbers and marks randomly – they do not have to relate to real students in your class. Each student structure must contain the following fields:
Student:
- First Name
- Surname
- Student Number
- Test Mark
Show how you would output just the test marks. How would you sort this structure array by test mark in descending order?
In this part, you are going to create a MATLAB Graphical User Interface (GUI) using GUIDE.
The GUI you need to create uses the function written in 'Part 3 - Plotting Projectile Motion' (see below). It allows a user to input various values for inputs x0, y0, V0x and V0y, and then the user can press a 'redraw plot' button to update the figure of projectile motion on the GUI. Your GUI should use the following UI components:
- Edit Text
- Static Text
- Axes
- Push Button
Some components may be used several times.

'Part 3':
function projectile(x0,y0,V0x,V0y)
g = -9.8;

b = V0y;
a = g/2
c = y0;
t_flight = (-b-sqrt(b^2-4*a*c))/(2*a);

t = linspace(0,t_flight,100);
x = x0 + V0x.* t;
y = y0 + V0y.* t + (1/2) * g.* t.^2;
plot(x, y, 'b-', 'Linewidth', 3);
axis equal;
xlabel('Distance (m)');
ylabel('Height (m)');
title('Projectile Motion');
Create a function to plot the output of projectile motion with inputs x0, y0, v0x and v0y. These correspond to the (x, y) coordinates of the projectile’s start location, as well as the initial velocities in each direction. You may assume zero acceleration in the x-direction, and acceleration in the y-direction is -g ݃ where g=9.81.
Use a time series vector (1D array) to denote time, and two vectors to denote the movement in the x-direction and y-direction. Your plot should only show the trajectory until the projectile object reaches the ground.
Create a 2D graph by plotting multiple data series on the same axes, using a resolution of at least 0.1 between points. Choose your axis limits so that all three mathematical functions show at least 2 full iterations. Ensure axes are all labelled, and each data series has a different line style, marker and colour.

y = f(x) = sin(x)
y = f(x) = cos(x)
y = f(x) = tan(x)
LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS