0% found this document useful (0 votes)
56 views

Mat3012 Numerical Methods (Lab Applicatoins) : Bahcesehir University

This document provides an overview of objectives and content covered in Lab 2 of the Numerical Methods course at Bahcesehir University. The objectives include learning about 2D graphics in MATLAB, creating M-files, defining simple functions, and working with polynomials. Examples are provided to demonstrate how to plot graphs, create script and function files, evaluate functions, and perform polynomial operations in MATLAB.

Uploaded by

drogo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Mat3012 Numerical Methods (Lab Applicatoins) : Bahcesehir University

This document provides an overview of objectives and content covered in Lab 2 of the Numerical Methods course at Bahcesehir University. The objectives include learning about 2D graphics in MATLAB, creating M-files, defining simple functions, and working with polynomials. Examples are provided to demonstrate how to plot graphs, create script and function files, evaluate functions, and perform polynomial operations in MATLAB.

Uploaded by

drogo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

BAHCESEHIR UNIVERSITY

MAT3012 NUMERICAL METHODS


(LAB APPLICATOINS)

Week-2, Spring

Resch. Asst. Aysun Soysal


LAB-2 OBJECTIVES

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

▪ MATLAB allows graphs to be created quickly and conveniently.

Some common commands in MATLAB for plotting:

• plot is used to create a graph of a given data


• xlabel is used to give a name to x-axis
• ylabel is used to give a name to y-axis
• title is used to give a name to the graph
• grid is used to specify the lines of coordinate axis.

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.

Days 11th 12th 14th 16th 17th


Temperature 10 15 30 0 5

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

• hold on plots more than one graph in one frame by superposing.

• subplot displays multiple polts on the same figure seperately.

• legend is used to show the names of the functions on the graph.

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.

Recall: The velocity of a free-falling bungee jumper can


be computed by
𝑔𝑚 𝑔𝑐𝑑
𝑣= 𝑡𝑎𝑛ℎ 𝑡
𝑐𝑑 𝑚

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)

iii. Save the file giving a name ‘scriptdemo’ ( in Documents to Matlab )


iv. Return to the command window and type the script name

>> 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:

function [outarg1, outarg2,…]=name(inarg1,inarg2,..)


Statements
outarg1=…
outarg2=…

! The function file must be saved as name.m.

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

iii. Save the file as freefall.m (Documents → Matlab)


iv. To invoke the function, return to the command window and type in

>> freefall( 12, 68.1, 0.25);


ans =
50.6175 18
(*II way)
i. Type the following statements in the file editor:

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))

ii. Save the file as freefalling.m (Documents → Matlab)


iii. To invoke the function, return to the command window and type
>> freefalling
Mass (kg): 68.1
Drag coefficient (kg/m): 0.25
Time(s): 12

Velocity (m/s): 19
50.6175
3. DEFINING SIMPLE FUNCTIONS ON MATLAB

You can define a simple function by ‘inline ( )’ command

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)

>> feval(f, [0 1])


ans =
1.0000 0.1111

>> f([0,1]) % II.way to evaluate f at x=0 ,x=1.


ans =
1.0000 0.1111

21
Ex2. Develop an m-file to calculate the value of the following function

𝑓 𝑥1 , 𝑥2 = 𝑥12 + 𝑥22 − 1, 2𝑥12 − 3𝑥22 − 2


at x=0 and x=1.

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

▪ A general form of a polynomial is


𝑎𝑛 𝑥 𝑛 + 𝑎𝑛−1 𝑥 𝑛−1 + ⋯ + 𝑎3 𝑥 3 + 𝑎2 𝑥 2 + 𝑎1 𝑥 1 + 𝑎0
▪ The corresponding matlab code of this polynomial :
𝑝 = 𝑎𝑛 𝑎𝑛−1 𝑎𝑛−2 … 𝑎3 𝑎2 𝑎1 𝑎0
▪ Some common commands in polynomial are:

• polyval (p, a) calculates the value of polynomial 𝑝(𝑥) at 𝑥 = 𝑎;


• conv p, q returns multiplication of two polynomials 𝑝(𝑥) and 𝑞 𝑥 ; i.e.; 𝑝 𝑥 𝑞 𝑥
𝑝(𝑥)
• deconv(p, q) returns the division of two polynomials; i.e.;
𝑞(𝑥)
• p n gives 𝑛𝑡ℎ element of vector 𝑝 𝑥 .
24
Ex. For the following polynomial 𝑝(𝑥)
𝑝 𝑥 = 𝑥 3 − 3𝑥 + 2
a) Find the first element of 𝑝 𝑥 ,
b) Find the value of 𝑝(𝑥) at 𝑥 = 1; i.e.; 𝑝 𝑥 = 1 ,
c) Find the value of 𝑝(𝑥) at 𝑥1 = −1, 𝑥2 = 0 and 𝑥3 = 1, respectively.

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

a) Find 𝑝 𝑥 𝑞 𝑥 and state the result as a polynomial.


𝑝(𝑥)
b) Find and state the result as a polynomial.
𝑞(𝑥)

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

𝑝 𝑥 𝑞 𝑥 = 5𝑥 7 + 25𝑥 6 − 16𝑥 5 − 5𝑥 4 + 𝑥 3 − 9𝑥 2 + 11𝑥 − 3

b)
>> deconv(p,q)
ans =
5 -25 139 -770
𝑝(𝑥)
= 5𝑥 3 − 25𝑥 2 + 139𝑥 − 770.
𝑞(𝑥) 28
THANK YOU FOR YOUR ATTENTION !

29

You might also like