EX1 Answers
EX1 Answers
EX1 Answers
1 Answers chapter 1
Answer 1.1
a) >> a = 21
The result of the sum 1 + 2 + 3 + 4 + 5 + 6 is ascribed to the variable a. De output
a = 21 appears in the MATLAB Command Window.
b) >> ()
The result of the sum 1 + 2 + 3 + 4 + 5 + 6 is ascribed to the variable b. The output is
not visible in the MATLAB Command Window because the semicolon sign (;) is used.
However, it is possible to call up the variable b.
c) >> ans = 24
The output of the sum b + 3 appears in the Matlab Command Window as ans = 24.
No variable is ascribed to this calculation. In such a case the result is automatically
ascribed to the variable ans.
d) >> ans = 21
Also in this exercise no variable is ascribed to the calculation. The output is thus as
follows: ans = 21.
e) >> c = 210
The result of this exercise depends on the previous answer because the variable ans is
used in this calculation. Assume that ans = 21 (obtained from exercise d), then the
answer on this question becomes: c = 210.
Answer 1.2
By using the key combination ‘ctrl-c’ you can interrupt a calculation or output in MATLAB.
The effect of the key combination ‘ctrl-c’ in this exercise is that the output is early canceled,
considering the fact that you apply the key combination in time.
OUTPUT SUMMARY:
>> 0:90000
key combination: ctrl-c
Answer 1.3
a) >> a = [2 3 4
The exercise a = [2, 3, 4 can be finished, after entering the ‘return’ key, by closing the
array yet with a bracket ] and again enter the ‘Return’ key. The array [2 3 4] is now
ascribed to the variable a.
b) >> ctrl-c ()
The exercise a = [2, 3, 4 can be interrupted by using the command ‘ctrl-c’. The calcu-
lation is then stopped and nothing is ascribed to the variable a.
120
Answer 1.4
Answer 1.5
Existing variables can be saved in a mat-file by using the command >> save filename.mat.
The file is written to the current active directory (see: ‘Current directory’ in the MATLAB
taskbar). Later on it is possible to call up the variables by loading the mat-file (pay atten-
tion that you are working in the directory in which MATLAB has written the mat-file) by
using the command >> load filename. Entering the command >> who gives a list with used
variables. Entering the command >> what gives a list with existing mat-files in that current
directory. The mat-file can be deleted by entering the command >> delete filename.mat.
INPUT SUMMARY:
>> f = 2
>> g = 3 + f
>> save try.mat f g
% Exit Matlab and start it again
>> who
>> what
>> delete try.mat
Answer 1.6
a) 0.1707
b) 2.5198
c) 0.7071
d) 1
Answer 1.7
First you have to enter >> x = 2. Then it is possible to enter the expressions by using the
input below.
Answer 1.8
a =
121
Columns 1 through 7
Columns 8 through 14
Columns 15 through 21
Answer 1.9
INPUT SUMMARY:
>> a = 9:-1:0
Answer 1.10
a) The error in this formulation is captured in the division (/). In this way a matrix-
wise division is used (which has a number as result) instead of an element-wise division
(which has an array as result).
b) The error message means that you used the power sign (∧ ) wrong. When you want to
calculate a matrix to the power n the matrix has to be square. The first term x^3 must
therefore be replaced with the term x.^3.
Answer 1.11
Enter the functions f . Pay attention that you make use of element-wise calculations (thus
with dots!).
b) >> f=(3.^x)./(1+3.^x)
Entering ‘f =’ is not necessary. Although it is useful when you want to separate different
equations.
122
Answer 1.12
>> nr = find(a<0.3)
Answer 1.13
Answer 1.14
>> prod(size(find(a>5)))
or
>> length(find(a>5))
a) ans =
b) ans =
Answer 1.15
You can calculate the sum of the elements by using the command sum. This command
gives you the sum of the elements in each row. To calculate the sum of the elements of the
matrices it is necessary to use this command twice.
You can obtain the number of elements in an array by using the command size. This com-
mand returns the dimensions of the array (2 numbers; number of rows, number of columns).
By multiplying these 2 numbers you get the number of elements of the array a.
123
PAY ATTENTION: You do not have to use a dot before the division sign (/) because this
time a division of 2 numbers is concerned.
Answer 1.16
Answer 1.17
Plotting a figure in MATLAB can be done in several ways. In this answer a plot is made by
typing several commands.
INPUT SUMMARY:
Answer 1.18
Answer 1.19
The reason that you can better save the commands for plotting a figure than the figure itself
is simply because then you can see how you obtained that figure. If you want to adjust the
124
figure in a later stadium then it is not necessary anymore to enter the commands again.
Answer 1.20
x = 0:0.1:2;
% Enter the array x with starting value 0 and final value 2
125
% and step size 0.1, without reproduction in the Matlab
% Command Window (;)
y = abs(x.*sin(2*pi*x));
% Enter the function: f(x) = |x*sin(2*pi*x)|
% also without reproduction in the MATLAB Command Window.
figure(1)
% Producing of figure 1
plot(x,y)
% Depicting the function y, with x on the x-axis and y on the y-axis.
title(’f(x) = |x*sin(2*pi*x)| on the interval [0,2]’)
% The title between the brackets is ascribed to the figure.
axis([0 2 0 2])
%% The axes of the figure are scaled by [xmin xmax ymin ymax]
shg
% The figure is presented on the foreground.
Answer 1.21
The file is saved in the current directory.On top of the MATLAB window you can find the
name of the current directory.
With the command >> dir you can call up the content of the current directory.
If you want to call up the function value of f for x = 1, it can be done as follows:
>> f(1)
ans =
3.7183
Answer 1.22
>> f(1)
y =
3.37183
ans =
3.7183
The output is not displayed on the screen when the semicolon (;) is used while the opposite
is true when the semicolon is left out.
Answer 1.23
In the following you will find a possible formulation for your own made function g(x) in
MATLAB.
126
function y = g(x)
a = zeros(size(x));
y = max(x,a);
The array a consists of zeroes (made with the MATLAB function zeros with the same di-
mensions as the array x.
With the command >> max you can compare the arrays x and a element-wise and that gives
you an array y that element-wise consists of the maximal values.
Thus:
if x(1) > a(1) then y(1) = x(1).
if x(1) < a(1) then y(1) = a(1).
Answer 1.24
- In part d) no error message arises because you call up the first element of array h. The
array h at that moment consists of one element with the value 2.5.
- Even if you enter a variable with the same name as in the previous defined function,
the function still exists.
Answer 1.25
In the MATLAB Command Window it is directly possible to obtain information about dif-
ferent functions by using the command help ‘keyword’. The specification that appears in
the MATLAB Command Window gives you specific information about the function which is
asked for.
This is an useful approach if you know which function you have to use. Hence, if you do not
know this you can better use the MATLAB Help Browser and then the header ”Search”.
INPUT SUMMARY:
>> help sin
>> help load
Answer 1.26
>> helpwin
then clicking:
127
→ Matlab
→ In Alphabetical Order
→ atan
Answer 1.27
This exercise can be solved in several ways. It is important that you find the way that is most
suited for you. In the following a possible solution is given.
INPUT SUMMARY:
→ You know that ‘sin’ is the MATLAB function name for the sine function, thus you are
going to use the MATLAB ”Help Browser” and the option ”Search”.
→ Click on the ‘yellow question mark’ in the MATLAB taskbar.
→ Select the header ”Search” in the ”Help Navigator”
→ Enter by ”Search for” the following: ‘sin’
→ ENTER
→ Now ‘sin’ appears in the the window below.
→ Choose for ‘sin’ and obtain the help information by double clicking.
Answer 1.28
ans =
Columns 1 through 5
Columns 6 through 10
Columns 11 through 15
Columns 16 through 20
Columns 21 through 25
Columns 26 through 30
128
Columns 31 through 35
Columns 36 through 40
Answer 1.29
>> 1.5511e+025
This question can be answered in several ways:
2. A more elegant approach, which is also easy to expand for numbers greater than 25, is
by writing a MATLAB script. First you need to define a row vector with the numbers
1 till 25. Then multiplying these elements with each other by means of the MATLAB
command prod, you get the desired answer.
Answer 1.30
>> 2686700
Answer 1.31
It is the best approach to write an m-file when you want to display a function in a figure.
Before you are going to calculate vectors or matrices, it is usual that you delete all current
defined variables with the MATLAB command clear.
To plot the function f (x) = x/(1+x) on the domain [0, 4] you first have to define a row vector
with elements. Then you need to calculate f (x) by means of element-wise calculations. Then
you can plot the vectors x and y by using the command plot.
If you want to embellish your figure you can make use of commands such as; title, xlabel,
ylabel and grid.
INPUT SUMMARY:
129
>> clear all
>> f = @(x)(x./(1+x));
>> x = 0:0.01:4;
>> figure
>> plot(x,f(x))
>> xlabel(’x’); ylabel(’f(x)’)
>> grid
Answer 1.32
1.5
0.5
−0.5
−1
−1.5
−2
−2 −1.5 −1 −0.5 0 0.5 1 1.5 2
Answer 1.33
You can define a new MATLAB function by opening a new m-file and the type
>> function [output] = function_name(input).
The function in this exercise must be able to handle with arrays so therefore you need to
multiply element-wise (thus with a dot!).
INPUT SUMMARY:
or
130
>> function y = f(x)
>> y = mod(x,2).^2
131
Input Output
a) (x^3)/6 1.3333
b) x/(sqrt(1+x^2)) 0.8944
c) 2^(1/3) 1.2599
d) exp(1+x^2) 148.4132
e) x^3*sin(x^2) -6.0544
f) atan(x)/(1+x^2) 0.2214
132