Chapter 13 - Differential Equation (1) Student
Chapter 13 - Differential Equation (1) Student
SCIENTIFIC
SKEE1022
DIFFERENTIAL
EQUATION
( WEEK 14 )
Syntax:
Handle creator
f = @(x) executable statement
Function Input
handle name argument
The output arguments are not define explicitly. The number of the output
arguments is depending on the executable statement type.
1) Mathematical expression: Single output argument.
2) Named Function: Similar to the output arguments of the named function.
Example 13.1
Below is a standard function save as .m file. Since the function only consist of a
single executable statement, the function can also be written as anonymous
function.
function f = mypoly(x)
f = x^2 + 1;
>> a = mypolyFH(2)
a =
5
Example 13.2
Writing available function as an anonymous function is a way to simplify the
function. For example, meshgrid is a function that accept vectors as input and
can return up to 3 output arguments. At certain situation, this function can be
simplified as anonymous function to accept scalars rather than vectors.
>> mygrid = @(x)meshgrid(0:x,0:2);
b =
0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
A function that accept function handle as its input argument is called function
functions.
Since anonymous function is also a function handle, it can be used as the input
to the function functions.
Later in this chapter, function integral() and ode45() are both the
example of function functions.
Creating function input for function functions should follow the input argument
requirement of the function functions.
For example, function integral() specify the function input as below:
Example 13.3
To plot 2 3 for 0: 0.1: 10, below is the MATLAB code when
using function plot():
>> x = 0:0.1:10;
>> y = 2*x.^2 + 3;
>> plot(x,y)
Alternatively, we can write a function file for the equation and use function
handle to call the function as below.
function y = myEq(x)
y = 2*x.^2+3;
Example 13.4
Below is MATLAB script showing c as the additional parameter to the
anonymous function.
for c = 10:5:25
subplot(2,2,c/5-1), fplot(@(x)2*x.^3-c*x.^2+2,[0 10])
xlabel('x')
ylabel('y = 2x^3 - cx^2 + 2')
title(['Plot for c = ' num2str(c)])
end
• Syntax
y = integral(fun,xmin,xmax)
Description
fun : Integrand, specified as a function handle, which defines the
function to be integrated from xmin to xmax.
For scalar‐valued problems, the function y = fun(x) must accept a
vector argument, x, and return a vector result, y. This generally
means that fun must use array operators instead of matrix
operators. For example, use .* (times) rather than * (mtimes). If
you set the 'ArrayValued' option to true, then fun must accept a
scalar and return an array of fixed size.
xmin : Lower limit of x.
xmax : Upper limit of x.
y : Integration result.
INTEGRAL
Example 13.5
Solve:
.
1 2 6 1 2 1
.
>> y1 = integral(@(x)2*x.^2+6*x+1,0,10)
y1 =
976.6667
>> y2 = integral(@(x)x.^2+1,5.5,16.1)
y2 =
1.3462e+03
Solution
1) Find by finding roots of when . Or, it is the roots of .
2) Find area under both the quadratic function, and linear function, for
from 0 to .
3) If area under graph is and area under graph is , area of the
shaded area is then
UNIVERSITI TEKNOLOGI MALAYSIA 18
DERIVATIVE FUNCTION: diff()/h
Syntax
Y = diff(X,n)/h^n %Approximate Derivatives
Description
X : Input array.
n : Derivative order.
h : Interval between data points.
Y : Difference result.
Example 13.7
Find derivative of 9.8 20.13 0.03 for 0 10.
h = 0.001;
t = 0:h:10;
y = 9.8*t.^2 + 20.13*t - 0.03;
dydt = diff(y)/h;
plot(t(2:end),[y(2:end); dydt])
xlabel('t')
title('Derivative Approximation Using diff()/h')
legend('y(t)','y''(t)’)
>> size(y)
ans =
1 10001
Note that the size of
>> size(dydt)
dydt is always shorter by
ans =
1 10000
1 sample compared to y.
Example 13.8
• Differentiate the following function for 0 1 using the diff()/h
function.
• Then, compare the results with the exact solution given by:
x = 0:0.001:10;
ydotexact = 20 - 360*x + 1950*x.^2 - 3520*x.^3 + 1800*x.^4;
plot(x(2:end),ydotexact(2:end),'k','LineWidth',1)
xlabel('x')
ylabel('y''(x)')
title('Derivative Approximation Error')
legend('h=1','h=0.5','h=0.1','Exact solution')
hold off
Example 13.9
• Solve and plot for the following function. Set time span from 0 to 50 .
4.0622 05 0.0036 0.0229 1.4151
• Then find 5 .
h=0.01;
t = 0:h:50;
y = -4.0622e-05*t.^4 + 0.0036*t.^3 - 0.0229*t.^2 + 1.4151*t;
ydot = diff(y,2)/h^2;
plot(t(3:end),ydot)
xlabel('x') 2nd order results is
ylabel('y''''(x)')
always shorter by 2
title('2^{nd} Order Derivative')
samples compared to
fprintf('y''''(5) = %.4f\n',ydot(5/h+1)) the original vector y.
y''(5) = 0.0502
Description
odefun : Function to be solve, specified as function handle.
tspan : Integral interval.
y0 : Initial condition.
y : Solution.
t : Evaluation points
Above is the description from the MATLAB documentation of the function handle
for function ode23().
The example given in the documentation can also be written as an anonymous
function as below:
@(t,y)5*y-3
Example 13.10
To find how the capacitor is charging, below is the differential equation of above
circuit derived from KCL
Solution
To solve the differential equation for using function ode23():
1) Rearrange the equation by setting the placed at the left side of the
equation as below and write the appropriate odefun function handle.
10
100 10
0.1
odefun = @(t,vc)100-10*vc
2) Set initial value for . In this example, it is set to 0.
3) Set time interval. In this example, it is set as [0 2].
4) Write the ode23() function and run the code.
plot(t,vc)
xlabel('t(s)'), ylabel('v_c (t)')
title('Charging of RC Circuit')
grid on
v (t)
c
It can be seen from the above figure, the steady state voltage of the capacitor is
10 , which is similar to the .
Example 13.11
Repeat example 13.10 with the following .
1 1
0
0
0 1 2
Solution
Above can be coded as 1*(t<1) or simply as (t<1). The only modification
needed for function odefun() is to replace the with the new equation
where the derivative equation is now ddt=((t<1)-vc)/0.1.
Solution
To solve the differential equation for using function ode23():
1) From Example 13.10, we have
10
In this example, is no longer a constant value where its value turns to 0
when 1. To solve this, we can code either using decision statement
or logical vector. Since anonymous function can only have one executable
statement, logical vector method is used in this example. Here can be
coded as 1*(t<1) or simply as (t<1) and the odefun function handle is
written as below:
odefun = @(t,vc)10*((t<1)-vc)
2) Similar to Example 13.10, initial value for is 0 and time interval is[0 2].
3) Write the ode23() function and run the code.
plot(t,vc)
xlabel('t(s)')
ylabel('v_c (t)')
title('Charging and Discharging of RC Circuit')
grid on
2 5 1
Then, the function ode23() is given with 2 initial values (one for and
one for ), specified as a vector. In this example the initial value is
specified by vector
inity = [ ]=[0 0]
tspan = [0 2];
inity = [0 0];
[t,y] = ode23(@odefun(t,y),inity,tspan);
1Ω 10
10
The differential equation of the above RLC circuit is as below where the
inductor contributed to the 2nd order differential equation. Plot for
equals to 50 , 100 and 635 .
Solution
To solve the differential equation for using function ode23():
1) Rearrange the equation by setting the placed at the left side of the
equation as below and write it in the function odefun().
10 /10
10 /10
3) Set initial value for and as [0 0]and time interval as[0 2].
4) Write the ode23() function and run the code.
5) is set as the additional parameter to the anonymous function since
function ode23() allowed only two inputs to the anonymous function.
System
Differential 1 1
0
Equation
Differential 1.4142Ω Ω
Equation Where Ω is the filter cutoff frequency in and is the filter gain