mmn_2_apieri_v2
mmn_2_apieri_v2
d027297@polito.it
a.pieri@isac.cnr.it
paolo.bardella@polito.it
MMN - 02 - Intro Matlab
Overview
3 Differential equations
MMN - 02 - Intro Matlab
Functions: plot, polynomials, integrals
Plan
3 Differential equations
MMN - 02 - Intro Matlab
Functions: plot, polynomials, integrals
Exercise 1
Using Matlab command polyfit, build the polynomial regression of
order n = 1, 2, 3 for the set of points which coordinates (x, y) are
given by
x=linspace(0,1.5,6);
y=[0,-0.0630,-0.1440,-0.0810,0.2880,1.1250];
Plot your results.
MMN - 02 - Intro Matlab
Functions: plot, polynomials, integrals
Exercise 1
>>p1=polyfit(x,y,1);
>>p2=polyfit(x,y,2);
>>p3=polyfit(x,y,3);
>>figure
>>plot(x,y,’o k’);
>>hold on;
>>plot(x,polyval(p1,x),’r’);
>>plot(x,polyval(p2,x),’b’);
>>plot(x,polyval(p3,x),’g’);
>>legend(’data’,’order 1’,’order 2’,’order 3’,’Location’,’Best’)
MMN - 02 - Intro Matlab
Functions: plot, polynomials, integrals
MMN - 02 - Intro Matlab
Functions: plot, polynomials, integrals
Exercise 2: spline
Plot the cubic spline interpolating the function 1/(1 + x2 ) at
n = 5, 9, 13 equidistant nodes on the interval [−5, 5] using the
Matlab command spline.
MMN - 02 - Intro Matlab
Functions: plot, polynomials, integrals
Exercise 2: spline
>>x1=linspace(-5,5,5);
>>x2=linspace(-5,5,9);
>>x3=linspace(-5,5,13);
>>f=@(x)1./(1+x.ˆ2);
>>y1=f(x1);y2=f(x2);y3=f(x3);
>>s1=spline(x1,y1);
>>s2=spline(x2,y2);
>>s3=spline(x3,y3);
>>x=linspace(-5,5,100);
>>figure
>>plot(x,f(x),’o’,’MarkerSize’,10);
>>hold on;
>>plot(x,ppval(s1,x),’r’,’LineWidth’,3);
>>plot(x,ppval(s2,x),’b’,’LineWidth’,3);
>>plot(x,ppval(s3,x),’g’,’LineWidth’,3);
>>legend(’exact’,’5 points’,’9 points’,’13 points’,’Location’,’Best’)
MMN - 02 - Intro Matlab
Functions: plot, polynomials, integrals
MMN - 02 - Intro Matlab
Functions: plot, polynomials, integrals
Exercise 5: integrals
Using Matlab commands quad and quadl compute the integrals
Z 1
I1 = exp(x)dx = e − 1
0
Z 1
I2 = cos(x)dx = sin(1)
0
MMN - 02 - Intro Matlab
Functions: plot, polynomials, integrals
Exercise 5: integrals
Using Matlab commands quad and quadl compute the integrals
Z 1
I1 = exp(x)dx = e − 1
0
Z 1
I2 = cos(x)dx = sin(1)
0
Exercise 5: integrals
>>f=@(x) exp(x);
>>I1=quad(f,0,1) % uses Simpson method
>>g=@(x) cos(x);
>>I2=quadl(g,0,1) % uses high order quadrature
MMN - 02 - Intro Matlab
Defining functions in files
Plan
3 Differential equations
MMN - 02 - Intro Matlab
Defining functions in files
myfun.m (solution 1)
function y=myfun(x)
% x : input variable
% y : output variable
y=ones(size(x));
y(x<=4)=x(x<=4)-3;
MMN - 02 - Intro Matlab
Defining functions in files
for k = 1:size(x,1)
for l = 1:size(x,2)
if x(k,l) <= 4
y(k,l) = x(k,l)-3;
else
y(k,l) = 1;
end
end
end
MMN - 02 - Intro Matlab
Defining functions in files
Exercise 6: solution 2
function [flag,d]=myfun2(A)
% function testing if a matrix is diagonal dominant
% A: input variable (a matrix)
% flag: output (0 if ’no’ or 1 if ’yes’)
flag=1;d=diag(A);% start assuming that A is diagonal dominant
for k=1:size(A,1)
tot=sum(abs(A(k,:)))-abs(A(k,k));
if tot>=abs(A(i,i))
flag=0;
break;
end
end
MMN - 02 - Intro Matlab
Defining functions in files
Exercise 6: solution 3
function [flag,d]=myfun3(A)
% function testing if a matrix is diagonal dominant
% A: input variable (a matrix)
% flag: output (0 if ’no’ or 1 if ’yes’)
d=diag(A);% start assuming that A is diagonal dominant
flag=all(2*abs(d)>sum(abs(A),2));
MMN - 02 - Intro Matlab
Defining functions in files
This disc
Pn has center aii in the complex plane and radius
Ri = j=1,j6=i |aij |.
Write a function that checks if an (input) value belongs to a
Gerschgorin disk Di of an (input) matrix for i = 1 . . . n.
MMN - 02 - Intro Matlab
Defining functions in files
Plan
3 Differential equations
MMN - 02 - Intro Matlab
Differential equations
Differential equations
Let I denote an (open) interval on the real line −∞ < t < +∞, and t0 ∈ I.
Let f be a real-valued continuous function on D = I × R. Consider the initial
value problem (
y 0 (t) = f (t, y(t))
y(t0 ) = y0
Theorem
Cauchy-Lipschitz. Suppose that f satisfies the Lipschitz condition i.e. ∃k > 0
such that
|f (t, y1 ) − f (t, y2 )| ≤ k|y1 − y2 |
for every (t, y1 ) and (t, y2 ) in D. Then there exists a unique function y(t) such
that
(i) y(t) ∈ C 1 (I)
(ii) y 0 (t) = f (t, y(t)) for each t ∈ I
(iii) y(t0 ) = y0
MMN - 02 - Intro Matlab
Differential equations
ode45
>>Tspan=[0 1]; % Solve from t = 0 to t = 1
>>IC=0; % y(t=0)=0
>>[T Y]=ode45(@(t,y) myode(t,y),Tspan,IC); % solve ode using
Runge-Kutta method
>>fplot(@(x) 0.5*(exp(x)-sin(x)-cos(x)),[0,1]);
>>hold on;
>>plot(T,Y,’o r’);
>>legend(’exact’,’ode45’,’Location’,’Best’);
MMN - 02 - Intro Matlab
Differential equations
Exercise 7
We consider the following Cauchy problem
(
y 0 (t) = −20 (y(t) − 1)2
(P)
y(0) = 2
Exercise 8
We consider the second order initial value problem relative to the harmonic
oscillator
2
ẍ + ω x = 0
(P) x(0) = x0
ẋ(0) = v0
q
k
where ω = m . Problem (P) also writes in matricial form
0 d x1 0 1 x1
(P ) = ,
dt x2 −ω 0 x2
file ’myode.m’
function dxdt = myode(t,x)
w=1;
gamma=0; % we neglect dissipation
dxdt = [x(2); -wˆ2*x(1)-2*gamma*x(2)];
Exercise 8
>>Tspan=[0,20];
>>ICs=[2;0]; % it’s a column vector
>>[t,x] = ode45(@myode,Tspan,ICs);
>>plot(t,x(:,1),’k’);
MMN - 02 - Intro Matlab
Differential equations
References