1
95 Matlab summary and exercises
We first give a summary of Matlab/Octave topics that you should be familiar with now. Then we
give a number of simple test questions. These are examples of the kind of Matlab/Octave question
that will be on the exam. Note that these are in the obligatory part of the exam (basic questions)
and you must answer all of them correctly.
1. Summary
Matlab/Octave is supposed to be a natural mathematical tool for you to use in the mathematics
courses and in the engineering courses. You will be expected to use Matlab/Octave without help
in these courses. Everone of the students must be able to do this, so here is a list of basic topics
that I want to emphasize now and that you must check that you are familiar with. Make sure
that you understand all of them.
• The use of semicolon ;.
• How to enter a matrix.
• How to enter a text string.
• How to enter floating point numbers, e.g., 1.55e3.
• Know the difference between matrix multiplication and elementwise multiplication, i.e., A*B
and A.*B .
• The command plot.
• The command feval.
• Iteration with two kinds of loop: while ... end and for ... end.
• The conditional command: if ... end
• Understand how functions work.
• Use the commands help and helpdesk.
• Two kinds of m-files: function file and script file.
• Finding simple test problems.
• Methods for finding errors (debugging); for example, compute the steps of the program by
hand, remove all semicolons and compare with what the program actually does.
• Never use numerical values in the program; use variables instead, initialize the variables at
the beginning of the computation. Simple example:
>> f=’funk3’; h=0.1; I=[0, 1]; u0=0;
>> [x,U]=my_ode(f,I,u0,h);
Two more topics that we have not used yet (to be done in later courses; you need not know
this yet):
• Use breakpoints for debugging.
• The command global.
This study source was downloaded by 100000852681095 from CourseHero.com on 12-12-2022 23:24:00 GMT -06:00
https://www.coursehero.com/file/55251727/MATLAB-test-questions-and-answers-
, 2
2. Exam questions
Make sure that you can do all of the following. (The >> denotes the prompt in the command
window.)
95.1. The file funk1.m is:
function z=funk1(x,y)
z=x/2+y/3;
What are the values of a and size(a) after the following:
>> x=3; y=6; z=8;
>> y=funk1(z,x);
>> a=[x, y, z];
95.2. Add the missing parts of the following program (marked by ??):
function [x,U]=my_int(f,I,ua,h)
% my_int - solves the initial value problem u’(x)=f(x), u(a)=ua
%
% Syntax:
% [x,U]=my_int(f,I,ua,h)
% Arguments:
% f - string containing the name of a function file,
% for example, f=’funk’
% I - 1x2 matrix, specifying an interval I=[a b]
% ua - real number, the initial value
% h - positive number, the stepsize
% Returns:
% x - a vector, the set of nodes x(i)
% U - a vector, U(i) is the approximate solution at
% the point x(i)
% Description:
% The program computes an approximate solution of the initial
% value problem u’(x)=f(x), a<x<b; u(a)=ua, according to
% the algorithm in the Fundamental Theorem of Calculus.
a=I(1);
b=I(2);
??
x(i)=a;
U(i)=ua;
while x(i)<b
i=i+1;
x(i)=??
U(i)=??
end
x=x’;
U=U’;
95.3. The file my trig.m is:
function [t,W]=my_trig(int,w0,h)
% my_trig - the solves initial value problem for the system of
% ordinary differential equations w’=Aw, A=[0 1;-1 0]
% Syntax:
% [t,W]=my_trig(int,w0,h)
This study source was downloaded by 100000852681095 from CourseHero.com on 12-12-2022 23:24:00 GMT -06:00
https://www.coursehero.com/file/55251727/MATLAB-test-questions-and-answers-
95 Matlab summary and exercises
We first give a summary of Matlab/Octave topics that you should be familiar with now. Then we
give a number of simple test questions. These are examples of the kind of Matlab/Octave question
that will be on the exam. Note that these are in the obligatory part of the exam (basic questions)
and you must answer all of them correctly.
1. Summary
Matlab/Octave is supposed to be a natural mathematical tool for you to use in the mathematics
courses and in the engineering courses. You will be expected to use Matlab/Octave without help
in these courses. Everone of the students must be able to do this, so here is a list of basic topics
that I want to emphasize now and that you must check that you are familiar with. Make sure
that you understand all of them.
• The use of semicolon ;.
• How to enter a matrix.
• How to enter a text string.
• How to enter floating point numbers, e.g., 1.55e3.
• Know the difference between matrix multiplication and elementwise multiplication, i.e., A*B
and A.*B .
• The command plot.
• The command feval.
• Iteration with two kinds of loop: while ... end and for ... end.
• The conditional command: if ... end
• Understand how functions work.
• Use the commands help and helpdesk.
• Two kinds of m-files: function file and script file.
• Finding simple test problems.
• Methods for finding errors (debugging); for example, compute the steps of the program by
hand, remove all semicolons and compare with what the program actually does.
• Never use numerical values in the program; use variables instead, initialize the variables at
the beginning of the computation. Simple example:
>> f=’funk3’; h=0.1; I=[0, 1]; u0=0;
>> [x,U]=my_ode(f,I,u0,h);
Two more topics that we have not used yet (to be done in later courses; you need not know
this yet):
• Use breakpoints for debugging.
• The command global.
This study source was downloaded by 100000852681095 from CourseHero.com on 12-12-2022 23:24:00 GMT -06:00
https://www.coursehero.com/file/55251727/MATLAB-test-questions-and-answers-
, 2
2. Exam questions
Make sure that you can do all of the following. (The >> denotes the prompt in the command
window.)
95.1. The file funk1.m is:
function z=funk1(x,y)
z=x/2+y/3;
What are the values of a and size(a) after the following:
>> x=3; y=6; z=8;
>> y=funk1(z,x);
>> a=[x, y, z];
95.2. Add the missing parts of the following program (marked by ??):
function [x,U]=my_int(f,I,ua,h)
% my_int - solves the initial value problem u’(x)=f(x), u(a)=ua
%
% Syntax:
% [x,U]=my_int(f,I,ua,h)
% Arguments:
% f - string containing the name of a function file,
% for example, f=’funk’
% I - 1x2 matrix, specifying an interval I=[a b]
% ua - real number, the initial value
% h - positive number, the stepsize
% Returns:
% x - a vector, the set of nodes x(i)
% U - a vector, U(i) is the approximate solution at
% the point x(i)
% Description:
% The program computes an approximate solution of the initial
% value problem u’(x)=f(x), a<x<b; u(a)=ua, according to
% the algorithm in the Fundamental Theorem of Calculus.
a=I(1);
b=I(2);
??
x(i)=a;
U(i)=ua;
while x(i)<b
i=i+1;
x(i)=??
U(i)=??
end
x=x’;
U=U’;
95.3. The file my trig.m is:
function [t,W]=my_trig(int,w0,h)
% my_trig - the solves initial value problem for the system of
% ordinary differential equations w’=Aw, A=[0 1;-1 0]
% Syntax:
% [t,W]=my_trig(int,w0,h)
This study source was downloaded by 100000852681095 from CourseHero.com on 12-12-2022 23:24:00 GMT -06:00
https://www.coursehero.com/file/55251727/MATLAB-test-questions-and-answers-