- Details
- Parent Category: Programming Assignments' Solutions

# We Helped With This MATLAB Programming Homework: Have A Similar One?

SOLVED

Category | Programming |
---|---|

Subject | MATLAB |

Difficulty | Undergraduate |

Status | Solved |

More Info | Homework Help Engineering |

## Short Assignment Requirements

Please solve the problems in the attached file using MATLAB, and send me the file back. Read all instructions in the file to understand how the professor wants this assignment to be solved.

## Assignment Code

```
% Biology 410/510: Analysis of Neural Data
% Instructor: Yashar Ahmadian
%
% Matlab Assignment 1
%
% Objective: The point of this exercise set is to
% 1) get you started with basic usage of the MATLAB language (inputing and manipulating vectors, plotting)
% 2) develop more intuition about vectors (and vector addition and scalar multiplication)
% by doing some simple exercises with them.
%
% ==================================================================================================
%
% Starting with MATLAB: (You can skip to line 35 if you're familiar with Matlab
% or if you attended the lab on April 5)
%
% Open your installed MATLAB. You can either enter code in the "Command Window"
% or you can open an editor window (within the MATLAB workspace) and
% start typing a program as a "script file" (if you open/create a new one make sure you save it!).
% The file you are reading now, with a ".m" extension, is a script file.
%
% MATLAB is an interpretor language, meaning it will execute the lines in your script file one after
% the other from start to end.
% The lines starting with % which you are reading now, which are (hopefully) appearing in
% green, are "comments" and they don't get executed.
% One good general programming advice is that always leave ample comments about any code you
% write, so later on you can more easily make sense of it.
% Simply write what each line does, or what you thought it does
% or meant it to do --look at my examples below.
%
% Remember (forever) that the most useful command in MATLAB is "help" followed by the name of a
% function that you want to learn how to use (if you don't know the name of a function the help command is useless,
% although you can always try to guess the name of a function you think may exist, such as "plot" --try entering "help plot"
% (without quotes) into your command line).
%
% Running Scripts:
% In lab (April 5) we mostly worked in the Command Window. But we also
% mentioned the possibility to write "scripts" in the editor window and
% run them there using either the "play" button or (better) the keyboard
% short-cut Command(?)-Enter (on Mac) or ctrl-Enter on Windows machines.
%
% Using double %% at the beginning of a line, you can create "sections" in
% your script code (as you will see below). When you're cursor is in one
% section, then command/ctrl-enter will only exectue that section of the
% code.
%
% ==================================================================================================
% General Homework Instructions (READ THEM!):
%
% 0. Sometimes I tell to you run a piece of code that I have put inside
% quotes (""). The code is what's inside and you should not type the "" when
% you try to run it.
%
% 1. In problems below, when I ask you to run a section of code (i.e. a part of code
% enclosed between two lines that each start with %%) you just move your
% cursor to that section and run it by pressing Command+Enter in mac or Ctrl+Enter in windows/Linux
% (or hit the "Play" button on top of Matlab's main window).
%
% (As you know, when the piece of code is just one line, you can also copy it to the command line
% in command window and hit enter to run it. But we will not do that in the above situation.
% By contrast, whenever I ask you to run help on something, I always mean to
% do that in the command window/command line.)
%
% 2. In some problems (or sub-problems) I ask you to write some code (this may be implicit).
% In that case use the "section" for that problem (or create a section, using %%, for that
% subproblem), and write your code there, and run it as described in #1 above.
%
% 3. In some problems, you may see "Q:" and "A:". This means you have to write an answer to the
% question in this same script file, in front of the "A:" prompt. The answer is based on the code
% you were asked to run and the results you obtained. You will write the answer as a
% MATLAB comment.
%
% 4. To submit your homework, after you have written all the answers and codes
% and done all the necessary modifications, save this script file (Matlab_HW1.m) and send
% it back to me in an email with subject "Bio 410/510: Matlab HW1".
%
% 5. If there are any figures that you must make as part of an exercise, save them as PDF files (using "save as" from
% the File menu of the figure window) and attach to the email you send.
% Name these figures as Matlab_HWx_Prob?.pdf, with x and ? replaced with
% appropriate numbers.
%================================================================================================
%%
%--------------------------------------------------------------------------------------------------
% Problem 1: More on Colon and transpose operators
% We saw how the colon operator ":" can be used to get a vector with components
% that are a sequence of consecutive integers. For example 3:6 generates the vector
% [3,4,5,6].
% In these sequences the step-size is 1.
% The colon operator can also be used to generate sequences with different step sizes.
% In the command window, run "help :" (without the "") to get help on :, and then use
% this operator to generate a list of numbers from 3 to 5 in steps of 0.2.
% Write code to assign this sequence/vector to variable V (using "V = ...").
% If the help is not immediately clear do some trial-and-error playing with
% : to figure it out. In general, trial-and-error play is the best way to
% learning to program (in MATLAB).
%
% Once that is done, use the ' operator (transposition) to create a
% column-vector version of V and call it W
%
% (Hint: If you could not figure it out, or forgot how we used ' in class, see the end of this long line for the answer: W = V' )
%
% Write your the two lines of code for generating V and W here (and run this section):
% Q: What is the length of V?
% A:
%%
%--------------------------------------------------------------------------------------------
% Problem 2: Concatenation
% concatenate V and 2*V horizontally, and assign it to Y.
% concatenate W and -W vertically and assing this to Z.
%
% Write the code here (and run it --see general instruction #1):
%%
%---------------------------------------------------------------------------------------------
% Problem 3: Matrices
%
% Using the same V and W created in Problem 1 (that should be in your workspace)
% concatenate V and 2*V vertically, and assign it to variable YY.
% concatenate W and -W horizontally, and assign it to variable ZZ.
% Also, using ', create the transposes of YY and ZZ and assign them to
% variables YY1 and ZZ1, respectively.
%
% Write the code here (and run it --see general instruction #1):
% Q: what is the size of YY, ZZ, YY1, and ZZ1, respectively? (use size())
%%
%--------------------------------------------------------------------------------------------
% Problem 4: Linspace
% Suppose you want to create a list of 50 numbers, that go from 3 to 5 in equal steps.
% You can do this using : as in problem 1, after figuring out what step-size will create
% a list with length 50. A more straightforward way is to use the function "linspace"
% Run "help linspace" and then use to generate the desired vector/list.
%
% write that line of code here:
%%
%--------------------------------------------------------------------------------------------
% Problem 5: Plotting (the cosine function)
% The number pi (~= 3.1415...) can be accessed in matlab via pi.
% For example
[-pi,pi]
%is a row vector of two components equal to + and - pi.
% Use linspace (see problem 4) to create a vector/list/sequence of 100 numbers going from
% 0 to 4*pi. Assign this vector to variable x. Type the code here:
% then run this piece of code:
y = cos(x);
% Run "help plot" (in command line) and use the x and y created above
% as the first two inputs to plot to plot the cosine function.
% Run "help xlabel" and "help ylabel" and "help title" (in command line)
% and give meaningful labels to your axes and the figure.
% (for example 'x' and 'cos(x)' and 'A plot of cos(x)')
%
% Don't forget to attach the figure to your email (See general instructions
% #5 above)!
%%
%--------------------------------------------------------------------------------------------
% Problem 6: "hold on" and "hold off"
%
% If you run two consecutive plot codes, the second plot will overwrite the
% first. You can use "hold on" and "hold off" (without quotes) to put several plots in the
% same figure.
% for example I will plot the functions, sin(x), cos(x) and sin(x)^2 + cos(x)^2 in
% the same plot below (you must first do problem 5)
figure(11);
clf;%this cleans the figure, in case it already existed
hold on;
plot(x,sin(x),'b');%by default teh plot line will be blue, but here we tell it, using the optional input 'b' to plot in blue
plot(x,cos(x),'r');%the option 'r' tells Matlab to plot the curve in red
plot(x,cos(x).^2 + sin(x).^2, 'g');
hold off;
ylim([-1.1,1.1]);% run "help ylim" (command line) to see what this does
% attach the figure to your email (See general instructions
% #5 above)!
%%
%--------------------------------------------------------------------------------------------
% Problem 7: Plotting points and vectors
%
% We can use the function plot to plot single points too.
% Remember that we can think of 2D vectors as specifying a point on the
% 2-dimensional plane, or as a 2D "arrow" that goes from the origin to that point.
%conside the vector
vec = [1;1.8]
%we can plot a point at that location using:
plot(vec(1),vec(2),'o');
% the optional input 'o' uses a o-marker to denote the point.
% You can also use '+', '*', etc (run "help plot" to see all options)
%note how I had to separete the first (or x) component of vec from its
%second (or y) component, because of the way the plot-function works.
% To plot something more similar to an arrow we have to somehow include the origin
% in the arguments to plot as well.
% The following block of code does this:
orig = [0 ; 0];%this is the origin as a column 0-vector --we could have also used zeros(2,1)
xs = [orig(1),vec(1)];% the list of x-components of the two points we want to connect to create an "arrow"
ys = [orig(2),vec(2)];% the list of y-components of the two points we want to connect to create an "arrow"
figure(12);
clf;
hold on;
plot(xs,ys,'b-');%this creates a blue line, not an arrow (in this case the optional input 'b-' is not necessary, because those are the default options)
%to make more arrow-like I will also add a tiny circle as the arrow-head:
plot(vec(1),vec(2),'bo'); %b is for color (blue) and o for shape
hold off;
axis([-2,2,-2,2]);%run "help axis" (on command line) to see what this does
grid on;%run "help grid" (on command line) to see what this does
% Now let's create two (normally distributed) random 2D vectors a and b
a = randn(2,1);
b = randn(2,1);
% Adopt and modify the code above (in this problem) to plot a and b and
% their sum (vector addition), all three as round-headed arrows as above.
% plot a and b in blue and their sum in red. (You may want to define new
% variables with new names)
%
% Write the code here below, and attach the figure to your email (See general instructions
% #5 above)!
% After you write your code Run this section a several times (like 10-20) to see different configurations, before
%saving your figure.
%%
%--------------------------------------------------------------------------------------------
% Problem 8: scalar multiplication, line segements, and 1D subspaces
%
figure(16);
hold on; %I write this at the begining without any "hold off" later, because I want
%everything plotted in the figure to stay in this case.
% As in the previous problem, define:
vec = [1; 1.8 ]
plot(vec(1),vec(2),'bo');
%let us also plot the origin
plot(0,0,'bo');
%Now we will multiply the vector vec with a random scalar
alpha = rand; %this gives you a random (scalar) number between 0 and 1
vec2 = alpha * vec; %scalar multiplication of scalar alpha with vector vec
plot(vec2(1),vec2(2),'ro'); %changed the color to red
axis ([-3,3,-3,3]);
%Run this section of code roughly 20 times.
% Q: If we were to run this infinite number of times, what would be
% set of all red points that we obtain (what I mean is characterize this
% set geometrically, in one sentence).
%
% A:
% Now modify the line of code that defines alpha, by replacing it with
% alpha = rand - 1;
% Q: Given what you know about rand (to remember run "help rand"), in what
% range of numbers can the modified alpha fall in?
%
% A:
%
% Q: run the code many times again with this modification.
% Characterize the obtained set of red points in this case.
%
% A:
% Q: Finally, what would happen if there no constraints on the alpha (the
% scalar multiplier), i.e. it could be any real number. What would be the
% obtain set of red points (with infinite runs) in that case?
%
% A:
% This is an example of a 1-dimensional subpace (of two dimensional space)
% More on that next week...
```