Matlab Exercise 1
Exercise 1: In this exercise, you will learn to write a simple script and a function to calculate the sum of two numbers and the uncertainty of the sum of the two numbers.
To complete this exercise, you may need to learn how to calculate the uncertainty.
a) Review the difference between script and a function.
Hints: https://www.mathworks.com/help/matlab/matlab_prog/scripts-and-functions.html
b) Write a script to calculate the uncertainty of the sum of a and b when a = 2, b = 3 and uncertainty of the a is ±0.1 and uncertainty of the b is ±0.2.
Hints: Go to New Menu in Matlab window, then click on script. Then in the script editor, type the following lines.
%%%%%%%%%%%%%%%%%%%%%% This program calculates uncertainty
% This is a comment line. In matlab comment line start with % symbol.
% When a line starts with "%", it does not perticipate into the calculation.
% It is always better to write comments.
% ALWAYS READ THE COMMENTS
% a and b - input variables
a = 2;
b = 3;
a_sigma = 0.1;
b_sigma = 0.2;
% c and c_sigma are output variable - the sum of a and B
c = a + b
% Calculate the uncertainty of the sum of a and b
% The line below is incomplete. Please complete this
c_sigma = ……………………………
%{ This is another way to add comments in your programs.
You do not need to add "%" in fron of every line.
Anyway, can you now identify the comment lines and program lines in a code?
If you can, do you understand why some program lines ends with "semicolon"(;) and some don't?
%}
Save this file as “cal_uncer_script”
Now, to run this script, go to command window and type “cal_uncer_script”;
It should print -
c =
5
c_sigma =
0.223606797749979
c) Now write a function to calculate the uncertainty of the sum of a and b, when uncertainty of the a is sigma_a and uncertainty of the b is sigma_b. Evaluate this function for a = 2, b = 3, a_sigma = ±0.1 and b_sigma = ±0.2.
Hints:
Go to New Menu in Matlab window, then click on “function”. Then in the script editor type the following lines.
function [c, c_sigma] = cal_uncer_function(a, b, a_sigma, b_sigma)
% On the left side -
% The function always start with the word "function"
% The you list all the output variables.
% On the right side -
% you write the funciton name and input variables
% For example -
% function [c, c_sigma] = cal_uncer_function(a, b, a_sigma, b_sigma)
% [ input ] function ( input )
% [ variables] name ( variables )
% This program calculates uncertainty
% Written By: Dhiman Mondal 2017-02-23
% Calculate the sum of a and b
c = a + b;
% Calculate the uncertainty of the sum of a and b
% The line below is incomplete. Please complete this
c_sigma = ……………………………;
% funciton always ends with "end" word
end
Save this file as “cal_uncer_function”
Now, to run this function, go to command window and type -
[c, c_sigma] = cal_uncer_function(2, 3, 0.1, 0.2)
It should print -
c =
5
c_sigma =
0.223606797749979
Notice that, using function you can use any number as input unlike a script which can do the same calculation but only for some specific numbers (we call it hardcoding or hardwiring).
c) Write a function to calculate the uncertainty of the sum of two columns of numbers from a data file given below. Finally plot the sum of the numbers with uncertainties.
Download the datafile: https://drive.google.com/open?id=1aXiqDt05k80BqjYGrh6cdOQcSqtUcOFk
Hints:
You may need read this: https://www.mathworks.com/help/matlab/matlab_prog/matlab-operators-and-special-characters.html
Find out what is the difference between a*a and a.*a
Go to New Menu in Matlab window, then click on “function”. Then in the script editor type the following lines.
function [c , cs] = cal_un_from_file(data)
%{ This function reads data from file
Do some calculation
and finally plot the values
%}
% Load the data file
data = load('data');
% Now you can see a variable called in "data" is workspace
% You can see that the size of "data" is 7 X 4
% That simply means "data" variable has 7 rows and 4 columns
% now, if you have to read
% a number from 1st column and 1st row you can type data(1,1)
% a number from 2nd column and 4th row you can type data(2,4)
% all numbers from 2nd column type data(:,2)
% Read the columns into variables
a = data(:,1); % Reading column 1
b = data(:,2); % Reading column 2
a_sigma = .........; % Complete this line to read column 3
b_Sigma = .........; % Complete this line to read column 4
% Sum of two numbers
c = a + b;
% uncertainty of the sum of two numbers
c_sigma = ................; % Complete this line
figure
errorbar(c,c_sigma)
xlabel('Number of Points')
ylabel(..................) % Complete this line to print ylabel 'Sum of Two numbers'
title(...................) % Complete this line to give a title
grid on
end
Now, to run this function, go to command window and type -
[c, c_sigma] = cal_un_from_file(data)
It should print
c =
4
2
3
5
8
10
6
c_sigma =
0.223606797749979
0.316227766016838
0.282842712474619
0.58309518948453
0.608276253029822
0.806225774829855
0.509901951359279
and also a figure such as ...
ZIP three matlab program files and figure, and then upload that using the form below.