Write a MATLAB code to solve the following difference equations by z-transform and display the solutions by stem plots:
1) An amount of USD 10,000 $ is deposited in a bank account with an annual interest
rate of 4%. Determine the balance of the account after 15 years.
2) Suppose that the number of bacteria in a colony doubles every hour. If a colony begins with five bacteria, how many will be present in n hours?
3) A bank account gives an interest rate of 5% compounded monthly. If you invest initially Rs .1000 and add Rs. 10 every month. How much money do you have after 5 years?
Definition: Z-transform
The Z-transform of a function f(n) is defined as
F(z)=∞n=0f(n)zn.
Concept: Using Symbolic Workflows
Symbolic workflows keep calculations in the natural symbolic form instead of numeric form. This approach helps you understand the properties of your solution and use exact symbolic values. You substitute numbers in place of symbolic variables only when you require a numeric result or you cannot continue symbolically. For details, see Choose Numeric or Symbolic Arithmetic. Typically, the steps are:
Workflow: Solve "Rabbit Growth" Problem Using Z-Transform
Declare Equations
You can use the Z-transform to solve difference equations, such as the well-known "Rabbit Growth" problem. If a pair of rabbits matures in one year, and then produces another pair of rabbits every year, the rabbit population p(n) at year n is described by this difference equation.
p(n+2) = p(n+1) + p(n).
Declare the equation as an expression assuming the right side is 0. Because n represents years, assume that n is a positive integer. This assumption simplifies the results.
syms p(n) z
assume(n>=0 & in(n,'integer'))
f = p(n+2) - p(n+1) - p(n)
f =
p(n + 2) - p(n + 1) - p(n)
Solve Equations
Find the Z-transform of the equation.
fZT = ztrans(f,n,z)
fZT =
z*p(0) - z*ztrans(p(n), n, z) - z*p(1) + z^2*ztrans(p(n), n, z) - ...
z^2*p(0) - ztrans(p(n), n, z)
The function solve solves only for symbolic variables. Therefore, to use solve, first substitute ztrans(p(n),n,z) with the variables pZT.
syms pZT
fZT = subs(fZT,ztrans(p(n),n,z),pZT)
fZT =
z*p(0) - pZT - z*p(1) - pZT*z - z^2*p(0) + pZT*z^2
Solve for pZT.
pZT = solve(fZT,pZT)
pZT =
-(z*p(1) - z*p(0) + z^2*p(0))/(- z^2 + z + 1)
Calculate p(n) by computing the inverse Z-transform of pZT. Simplify the result.
pSol = iztrans(pZT,z,n);
pSol = simplify(pSol)
pSol =
2*(-1)^(n/2)*cos(n*(pi/2 + asinh(1/2)*1i))*p(1) + ...
(2^(2 - n)*5^(1/2)*(5^(1/2) + 1)^(n - 1)*(p(0)/2 - p(1)))/5 - ...
(2*2^(1 - n)*5^(1/2)*(1 - 5^(1/2))^(n - 1)*(p(0)/2 - p(1)))/5
Substitute Values
To plot the result, first substitute the values of the initial conditions. Let p(0) and p(1) be 1 and 2, respectively.
pSol = subs(pSol,[p(0) p(1)],[1 2])
pSol =
4*(-1)^(n/2)*cos(n*(pi/2 + asinh(1/2)*1i)) - (3*2^(2 - n)*5^(1/2)* ...
(5^(1/2) + 1)^(n - 1))/10 + (3*2^(1 - n)*5^(1/2)*(1 - 5^(1/2))^(n - 1))/5
Plot Results
Show the growth in rabbit population over time by plotting pSol.
nValues = 1:10;
pSolValues = subs(pSol,n,nValues);
pSolValues = double(pSolValues);
pSolValues = real(pSolValues);
stem(nValues,pSolValues)
title('Rabbit Population')
xlabel('Years (n)')
ylabel('Population p(n)')
grid on
Comments
Leave a comment