Mat3012 Numerical Methods (Lab Applicatoins) : Bahcesehir University
Mat3012 Numerical Methods (Lab Applicatoins) : Bahcesehir University
Week-2, Spring
1. 2-D Graphics
2. Learning How to Create an M-file
3. Defining Simple Functions on MATLAB
4. Working with Polynomials
2
1. 2D GRAPHICS
3
Graphic Line Specifications Used in the plot() Command
4
Ex1. Suppose the data given in the table are the highest temperatures measured on the day
11𝑡ℎ, 12𝑡ℎ, 14𝑡ℎ, 16𝑡ℎ, and 17𝑡ℎ days, respectively. Plot the highest temperatures on the days.
5
Solution.
x=[11 12 14 16 17];
y=[10 15 30 0 5];
plot(x,y,'b--d')
xlabel('days')
ylabel('temperature’)
grid on
title('the highest temperatures
on the days')
6
• clf clears the current figure window
7
1 1 1 1
Ex2. Consider the functions 𝑓 𝑥 = , 𝑔 𝑥 = , 𝑣 𝑥 = , and ℎ 𝑥 =
1+𝑒 −𝑥 1+𝑒 −2𝑥 1+𝑒 −4𝑥 1+𝑒 −6𝑥
on the interval −4,4 with 32 equal spaced points. The graph of 𝑓(𝑥) is a black solid line with a
square marker, the graph of 𝑔(𝑥) is magenta dashed line with diamand marker, the graph of 𝑣(𝑥)
is a blue dotted line with plus marker, and the graph of ℎ(𝑥) is green dash-dot line with circle
marker. The title of the resultant figure is ‘Approximation of the Heaviside step function’. Then,
draw the graphs of these functions with given properties as :
a) a single frame (by superposing)
b) a composite frame 4x1 matrix
c) a composite frame 1x4 matrix
d) a composite frame 2x2 matrix
8
Soltion.
a)
x=linspace(-4,4,32);
f=1./(1+exp(-x));
g=1./(1+exp(-2*x));
v=1./(1+exp(-4*x));
h=1./(1+exp(-6*x));
plot(x,f,'k-s')
hold on
plot(x,g,'m--d')
hold on
plot(x,v,'b:+')
hold on
plot(x,h,'go-.')
legend('f(x)','g(x)','v(x)','h(x)')
title('Approximations of the Heaviside step function') 9
b)
x=linspace(-4,4,32);
f=1./(1+exp(-x));
g=1./(1+exp(-2*x));
v=1./(1+exp(-4*x));
h=1./(1+exp(-6*x));
subplot(4,1,1)
plot(x,f,'k-s')
title('f(x)')
subplot(4,1,2)
plot(x,g,'m--d')
title('g(x)')
subplot(4,1,3)
plot(x,v,'b:+')
title('v(x)')
subplot(4,1,4)
plot(x,h,'go-.') 10
title('h(x)')
c)
x=linspace(-4,4,32);
f=1./(1+exp(-x));
g=1./(1+exp(-2*x));
v=1./(1+exp(-4*x));
h=1./(1+exp(-6*x));
subplot(1,4,1)
plot(x,f,'k-s')
title('f(x)')
subplot(1,4,2)
plot(x,g,'m--d')
title('g(x)')
subplot(1,4,3)
plot(x,v,'b:+')
title('v(x)')
subplot(1,4,4)
plot(x,h,'go-.') 11
title('h(x)')
d)
x=linspace(-4,4,32);
f=1./(1+exp(-x));
g=1./(1+exp(-2*x));
v=1./(1+exp(-4*x));
h=1./(1+exp(-6*x));
subplot(2,2,1)
plot(x,f,'k-s')
title('f(x)')
subplot(2,2,2)
plot(x,g,'m--d')
title('g(x)')
subplot(2,2,3)
plot(x,v,'b:+')
title('v(x)')
subplot(2,2,4)
plot(x,h,'go-.')
12
title('h(x)')
2. LEARNING HOW TO CREATE AN M-FILE
An m-file consists of a series of statements that can be run all at once. There are two types of
M-files :
• Script file
• Function file
2.1 Script File. A script file is merely a series of MATLAB commands that are saved on a file.
The script can be executed by typing the file name in the command window.
13
Ex1. Develop a script file to compute the velocity of the free-falling bungee jumper for the case
where the initial velocity is zero; for this case
𝑚
• the acceleration due to gravity (𝑔) is 9.81 2 .
𝑠
• the mass (𝑚) is 68.1 𝑘𝑔
• the drag coefficient 𝑐𝑑 is 0.25 𝑘𝑔/𝑚
• the time (𝑡) is 12 s.
where 𝑣 is velocity. 14
Solution.
i. Open the editor with the menu bar selection: New, Script.
ii. Type the statements on the editor
g=9.81;
m=68.1;
t=12;
cd=0.25;
v=sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t)
>> scriptdemo
v=
50.6175 15
2.2 Function File. A function file is an m-file that starts with the word ‘function’. In contrast to
script files, it accepts input arguments and return outputs. Its syntax:
16
Ex2. Compute the velocity of the free-falling bungee jumper by using a function file for the task
when the values are
𝑘𝑔
𝑡 = 12 𝑠 ; 𝑚 = 68.1 𝑘𝑔 ; 𝑐𝑑 = 0.25 .
𝑚
17
Solution.
(*I.way)
i. Open a function file with the menu selection. New, Function One advantage of a
ii. Type the statements in the file editor: function m-file is that it can
be invoked repeatedly for
different argument values:
function [ v ] = freefall( t, m, cd ) >> freefall(8,100,0.25)
g = 9.81; ans =
v = sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t); 53.1878
end
function freefalling
g = 9.81;
m = input('Mass (kg):’);
cd = input('Drag coefficient (kg/m):’);
t = input('Time(s):’);
disp(' ‘)
disp('Velocity (m/s):’)
disp(sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t))
Velocity (m/s): 19
50.6175
3. DEFINING SIMPLE FUNCTIONS ON MATLAB
1
Ex1. Evaluate the function 𝑓 𝑥 = 1+8𝑥2 for 𝑥 = 0 and 𝑥 = 1.
20
Solution.
>> f=inline('1./(1+8*x.^2)','x')
f=
Inline function:
f(x) = 1./(1+8*x.^2)
21
Ex2. Develop an m-file to calculate the value of the following function
22
Solution.
function [ y ] = myfunc( x )
y(1)=x(1)*x(1)+x(2)*x(2)-1;
y(2)=2*x(1)*x(1)-3*x(2)*x(2)-2;
end
>> x=[0,1];
>> myfunc(x)
ans =
0 -5
23
4. WORKING WITH POLYNOMIALS
25
Solution:
>> p=[1 0 -3 2];
>> p(1) % gives the first element of p(x)
ans =
1
>> polyval(p,1) % the value of p(x) at x=1
ans =
0
>> polyval(p,[-1 0 1]) % the value of p(x) at x=-1, x=0 and x=1
ans =
4 2 0 26
Ex2. For the following polynomials 𝑝(𝑥) and 𝑞 𝑥
𝑝 𝑥 = 5𝑥 5 − 𝑥 3 − 2𝑥 + 1
𝑞 𝑥 = 𝑥 2 + 5𝑥 − 3
27
Solution.
a)
>> p=[5 0 -1 0 -2 1];
>> q=[1,5,-3];
>> conv(p,q)
ans =
5 25 -16 -5 1 -9 11 -3
b)
>> deconv(p,q)
ans =
5 -25 139 -770
𝑝(𝑥)
= 5𝑥 3 − 25𝑥 2 + 139𝑥 − 770.
𝑞(𝑥) 28
THANK YOU FOR YOUR ATTENTION !
29