matlab homework
10/8/2014
1
MATLAB Introduction II
Programming and Scripts
Programming and Scripts
• The simplest type of MATLAB program is
called a script.
• A script is a file with a .m extension that
contains multiple sequential lines of MATLAB
commands and function calls.
• You can run a script by typing its name at the
command line.
10/8/2014
2
Sample Script
• To create a script, use the edit command,
• This opens a blank file named plotrand.m
• Enter some code that plots a vector of random data:
edit plotrand
n = 50;
r = rand(n,1);
plot(r)
Sample Script • Next, add code that draws a horizontal line on
the plot at the mean:
m = mean(r);
hold on
plot([0,n],[m,m])
hold off
title('Mean of Random Uniform Data')
10/8/2014
3
Sample Script • Whenever you write code, it is a good practice to add
comments that describe the code. Comments allow others to
understand your code, and can refresh your memory when you
return to it later. Add comments using the percent (%) symbol.
% Generate random data from a uniform distribution
% and calculate the mean. Plot the data and the mean.
n = 50; % 50 data points
r = rand(n,1);
plot(r)
% Draw a line from (0,m) to (n,m)
m = mean(r);
hold on
plot([0,n],[m,m])
hold off
title('Mean of Random Uniform Data')
Sample Script
• Save the file in the current folder. To run the
script, type its name at the command line:
• You can also run scripts from the Editor by
pressing the Run button, .
plotrand
10/8/2014
4
Sample Script
• You will have something like this.
0 5 10 15 20 25 30 35 40 45 50 0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Mean of Random Uniform Data
Loops and Conditional Statements
• Within a script, you can loop over sections of code
and conditionally execute sections using the
keywords for, while, if, and switch.
• For example, create a script named calcmean.m that
uses a for loop to calculate the mean of five random
samples and the overall mean.
edit calcmean.m
10/8/2014
5
Loops and Conditional Statements
• Now, modify the for loop so that you can view the results at each iteration. Display text in the Command Window that includes the current iteration number, and remove the semicolon from the assignment to sampleMean.
nsamples = 5;
npoints = 50;
for k = 1:nsamples
currentData = rand(npoints,1);
sampleMean(k) = mean(currentData);
end
overallMean = mean(sampleMean)
Loops and Conditional Statements
• When you run the script, it displays the
intermediate results, and then calculates the
overall mean.
for k = 1:nsamples
iterationString = ['Iteration #',int2str(k)];
disp(iterationString)
currentData = rand(npoints,1);
sampleMean(k) = mean(currentData)
end
overallMean = mean(sampleMean)
10/8/2014
6
Loops and Conditional Statements • You will have something like this in the command
window. Iteration #1
sampleMean =
0.4899
Iteration #2
sampleMean =
0.4899 0.4505
Iteration #3
sampleMean =
0.4899 0.4505 0.4844
Iteration #4
sampleMean =
0.4899 0.4505 0.4844 0.4642
Iteration #5
sampleMean =
0.4899 0.4505 0.4844 0.4642 0.5447
overallMean =
0.4867
Loops and Conditional Statements
• In the Editor, add conditional statements to
the end of calcmean.m that display a different
message depending on the value of
overallMean.
if overallMean < .49
disp('Mean is less than expected')
elseif overallMean > .51
disp('Mean is greater than expected')
else
disp('Mean is within the expected range')
end
10/8/2014
7
Loops and Conditional Statements
• Run calcmean and verify that the correct
message displays for the calculated
overallMean. For example:
overallMean =
0.5178
Mean is greater than expected
Script Locations • MATLAB looks for scripts and other files in certain
places. To run a script, the file must be in the current folder or in a folder on the search path.
• By default, the MATLAB folder that the MATLAB Installer creates is on the search path. If you want to store and run programs in another folder, add it to the search path. Select the folder in the Current Folder browser, right-click, and then select Add to Path.
10/8/2014
8
Homework 1. Write a script named Homeworka2.m with the following
information using a for loop with x ranging from x = -5 to 5
� = �� + 3�� − 10� − 24
Plot (x,y) and label x-, y-axes and title of the graph. Print your
script and your plot.
2. Write a script named using j = rand(50, 1) from last to
generate 50 random numbers. In your script find max(j), min(j),
median(j) std(j) mean(j). End your script with this
Print the script and command window displayed answer
if median(j) > mean(j)
disp('Mean is less than Median!')
elseif median(j) < mean(j)
disp('Mean is greater than Median! ')
elseif abs(median(j)- mean(j))<0
disp('there is no difference!')
end