How to solve for the impulse response using a differential equation? - matlab

Given a differential equation:
y[n] - 0.9y[n-1] + 0.81y[n-2] = x[n] - x[n-2]
a. Find the impulse response for h[n], n=0,1,2 using recursion.
b. Find the impulse response using MATLAB command filter.

I understand that this is homework, so I will try to give you guidelines without actually giving away the answer completely:
Using recursion
This is actually quite simple, because the differential equation contains the body of the recursive function almost entirely: y[n] = 0.9y[n-1] - 0.81y[n-2] + x[n] - x[n-2]
The parts in bold are actually the recursive calls! What you need to do is to build a function (let's call it func) that receives x and n, and calculates y[n]:
function y = func(x, n)
if (n < 0)
%# Handling for edge case n<0
return 0
else if (n == 0)
%# Handling for edge case n=0
return x(0)
else
%# The recursive loop
return 0.9 * func(x, n-1) - 0.81 * func(x, n-2) + x(n) - x(n-2)
end
Note that it's pseudo-code, so you still have to check the edge cases and deal with the indexation (indices in MATLAB start with 1 and not 0!).
Using filters
The response of a digital filter is actually the y[n] that you're looking for. As you probably know from lesson, the coefficients of that filter would be the coefficients specified in the differential equation. MATLAB has a built-in function filter that emulates just that, so if you write:
B = [1, 0, 1]; %# Coefficients for x
A = [1, 0.9, -0.81]; %# Coefficients for y
y = filter(B, A, x);
You'd get an output vector which holds all the values of y[n].

a=[1 -0.9 0.81]
b=[1 -1]
impz(b,a,50)

Related

I am not getting this line of code in Machine Learning

I have to write this piece of code for the lrcostfunction assignment in the Machine Learning course in coursera. But I still don't understand why
theta1 = [0 ; theta(2:end, :)];
is written? theta1 means what?
h = sigmoid(X * theta)
theta1 = [0 ; theta(2:end, :)];
p = lambda * (theta1' * theta1)/(2 * m);
J = ((-y)'*log(h)-(1-y)'*log(1-h))/m + p;
grad = (X' * (h - y) + lambda * theta1)/ m;
In logistic regression, theta (θ) is a vector representing the parameters (or weights) of the linear function of x.
Now, given a training set, one method to learn the parameters theta (θ) is to be to make h(x) close to y, at least for the training examples we have. This is defined using a cost function or the error function (J(θ)), for each value of the θ, which we want to minimize.
The first theta1 parameter is initialized as zero. Later using gradient descent, next theta parameter is computed. In gradient descent, the J(θ) parameter is calculated using partial differentiation as we want to minimize it.
Here \alpha is learning rate with which gradient descent algorithm runs. It starts with an initial value in the array - theta1 as zero and then, next value is calculated using the above equation. and so on for other theta parameters.
EDIT:
Explaining the code:
theta1 = [0 ; theta(2:end, :)];
The above code is MATLAB code. Here theta1 is an Array (vector or matrix representation). It is created using horizontal concatenation of two fields.
1) 0
2) theta(2:end, :)
First, is a scalar value 0
Second, this means that take all values as it is, except the first row from the array theta. (Note theta is input array to LRCOSTFUNCTION(theta, X, y, lambda))

How to generate frequency response given b,a coefficients of the system?

I have the following system, specified by the set of coefficients:
b = [1 2 3];
a = [1 .5 .25];
In the Z-Domain, such function will have the following transfer function:
H(Z) = Y(Z)/X(Z)
So the frequency response will be just the unit circle, where:
H(e^jw) = Y(e^jw)/X(e^jw)
Do I just substitute in the e^jw for 'Z' in my transfer function to obtain the frequency response of the system mathematically, on paper? Seems a bit ridiculous from my (a student's) point of view.
Have you tried freqz()? It returns the frequency response vector, h, and the corresponding angular frequency vector, w, for the digital filter with numerator and denominator polynomial coefficients stored in b and a, respectively.
In your case, simply follow the help:
[h,w]=freqz(b,a);
You do sub in e^jw for Z. This isn't ridiculous. Then you just sweep w from -pi to pi. Your freq response will be the absolute value of the result.
As Alessiox mentioned, freqz is the command you want to use in matlab.
I would indeed be as simple as substituting exp(j*w) in your transfer function. There are of course different ways to implement this with Matlab. For the purpose of illustration, I will be assuming bs are the coefficients of the x sequence and as are the coefficients of the y sequence, such that the b are in the numerator and the as are in the denominator:
A direct evaluation with Matlab could be done with:
b = [1 2 3];
a = [1 .5 .25];
N = 513; % number of points at which to evaluate the transfer function
w = linspace(0,2*pi,N);
num = 0;
for i=1:length(b)
num = num + b(i) * exp(-j*i*w);
end
den = 0;
for i=1:length(a)
den = den + a(i) * exp(-j*i*w);
end
H = num ./ den;
This would be equivalent to the following which makes use of the builtin polyval:
N = 513; % number of points at which to evaluate the transfer function
w = linspace(0,2*pi,N);
H = polyval(fliplr(b),exp(-j*w))./polyval(fliplr(a),exp(-j*w));
Also, this is really evaluating the transfer function at discrete equally spaced angular frequencies w = 2*pi*k/N which corresponds to the Discrete Fourier Transform (DFT). As such it could also be done with:
N = 512;
H = fft(b,N) ./ fft(a,N);
Incidentally this is what freqz does, so you could also get the same result with:
N = 512;
H = freqz(b,a,N,'whole');

USE DIFFERENTIAL MATRIX OPERATOR TO SOLVE ODE

We were asked to define our own differential operators on MATLAB, and I did it following a series of steps, and then we should use the differential operators to solve a boundary value problem:
-y'' + 2y' - y = x, y(0) = y(1) =0
my code was as follows, it was used to compute this (first and second derivative)
h = 2;
x = 2:h:50;
y = x.^2 ;
n=length(x);
uppershift = 1;
U = diag(ones(n-abs(uppershift),1),uppershift);
lowershift = -1;
L= diag(ones(n-abs(lowershift),1),lowershift);
% the code above creates the upper and lower shift matrix
D = ((U-L))/(2*h); %first differential operator
D2 = (full (gallery('tridiag',n)))/ -(h^2); %second differential operator
d1= D*y.'
d2= ((D2)*y.')
then I changed it to this after posting it here and getting one response that encouraged the usage of Identity Matrix, however I still seem to be getting no where.
h = 2;
n=10;
uppershift = 1;
U = diag(ones(n-abs(uppershift),1),uppershift);
lowershift = -1;
L= diag(ones(n-abs(lowershift),1),lowershift);
D = ((U-L))/(2*h); %first differential operator
D2 = (full (gallery('tridiag',n)))/ -(h^2); %second differential operator
I= eye(n);
eqn=(-D2 + 2*D - I)*y == x
solve(eqn,y)
I am not sure how to proceed with this, like should I define y and x, or what exactly? I am clueless!
Because this is a numerical approximation to the solution of the ODE, you are seeking to find a numerical vector that is representative of the solution to this ODE from time x=0 to x=1. This means that your boundary conditions make it so that the solution is only valid between 0 and 1.
Also this is now the reverse problem. In the previous post we did together, you know what the input vector was, and doing a matrix-vector multiplication produced the output derivative operation on that input vector. Now, you are given the output of the derivative and you are now seeking what the original input was. This now involves solving a linear system of equations.
Essentially, your problem is now this:
YX = F
Y are the coefficients from the matrix derivative operators that you derived, which is a n x n matrix, X would be the solution to the ODE, which is a n x 1 vector and F would be the function you are associating the ODE with, also a n x 1 vector. In our case, that would be x. To find Y, you've pretty much done that already in your code. You simply take each matrix operator (first and second derivative) and you add them together with the proper signs and scales to respect the left-hand side of the ODE. BTW, your first derivative and second derivative matrices are correct. What's left is adding the -y term to the mix, and that is accomplished by -eye(n) as you have found out in your code.
Once you formulate your Y and F, you can use the mldivide or \ operator and solve for X and get the solution to this linear system via:
X = Y \ F;
The above essentially solves the linear system of equations formed by Y and F and will be stored in X.
The first thing you need to do is define a vector of points going from x=0 to x=1. linspace is probably the most suitable where you can specify how many points we want. Let's assume 100 points for now:
x = linspace(0,1,100);
Therefore, h in our case is just 1/100. In general, if you want to solve from the starting point x = a up to the end point x = b, the step size h is defined as h = (b - a)/n where n is the total number of points you want to solve for in the ODE.
Now, we have to include the boundary conditions. This simply means that we know the beginning and ending of the solution of the ODE. This means that y(0) = y(1) = 0. As such, we make sure that the first row of Y has only the first column set to 1 and the last row of Y has only the last column set to 1, and we'll set the output position in F to both be 0. This symbolizes that we already know the solution at these points.
Therefore, your final code to solve is just:
%// Setup
a = 0; b = 1; n = 100;
x = linspace(a,b,n);
h = (b-a)/n;
%// Your code
uppershift = 1;
U = diag(ones(n-abs(uppershift),1),uppershift);
lowershift = -1;
L= diag(ones(n-abs(lowershift),1),lowershift);
D = ((U-L))/(2*h); %first differential operator
D2 = (full (gallery('tridiag',n)))/ -(h^2);
%// New code - Create differential equation matrix
Y = (-D2 + 2*D - eye(n));
%// Set boundary conditions on system
Y(1,:) = 0; Y(1,1) = 1;
Y(end,:) = 0; Y(end,end) = 1;
%// New code - Create F vector and set boundary conditions
F = x.';
F(1) = 0; F(end) = 0;
%// Solve system
X = Y \ F;
X should now contain your numerical approximation to the ODE in steps of h = 1/100 starting from x=0 up to x=1.
Now let's see what this looks like:
figure;
plot(x, X);
title('Solution to ODE');
xlabel('x'); ylabel('y');
You can see that y(0) = y(1) = 0 as per the boundary conditions.
Hope this helps, and good luck!

Computing coefficients of FIR filter in Matlab

I have to create the function G(z) = [3*H^2(z)-2H^3(z)]*(z^-2) which takes as an input the impulse response of the filter H(z) , and outputs the impulse response of G(z).
I assume H(z) is a generic FIR filter
b = fir1(10,0.5);
h = impz(b);
t = impzlength(b);
where h is the values of the impulse response.
I think H^2(z) = h(n).*z(-2n) and H^3(z) = h(n).*z^(-3n); H(z) is the transfer function of the filter .
I have to calculate the coefficients of num and den of the equation now, but I am stuck.
I thought at first to use coeffs and a for loop but i need also the zero coefficients, while coeffs only provides the non zero coefficients.
Now I thought that maybe there is a work-around for obtaining the coefficients: basically I have to select only certain values of h.
For example, to obtain the coefficients only for z^-3n:
n = 3;
y = h(n:n:end); % = 3 6 9 12 ...
But now I can't figure out how to sum appropriately the coefficients for z^-3n and z^-2n.
Unless you are using a non-standard notation, H^2(z) is not h(n).*z(-2n) but rather the multiplication of a polynomial with coefficients h with itself. This can be computed with:
H2 = conv(h, h);
Similarly, H^3(z) can be computed using:
H3 = conv(H2, h);
Then, summing the polynomials boils down to summing the coefficients, with the only catch that you have to pad H2 so that the two vectors of coefficients have the same size:
H2 = [H2 zeros(1,length(H3)-length(H2))];
S = 3*H2 -2*H3;
The final multiplication by z^(-2) (which can be represented by the polynomial coefficients [0 0 1]) could be achieve in the same way using conv with:
G = conv(S, [0 0 1 zeros(1,length(Sum)-3)]);
or alternatively, you may realize that multiplying by a single term polynomial is essentially equivalent to shifting the coefficients:
G = [0 0 S];

Writing a matlab function that implements Euler's method?

I should write a MATLAB function that takes a first order ordinary differential equation in form y’(t) = a*y(t) +b with an initial point y(t0)=y0 as inputs and calculates first 15 points of the solution. Also draws the solution curve for first 15 points.
And the equation that we want to solve is ;y’(t) = 4*y(t)+1 with the initial point y(0)=0.
For this function I wrote the bellowing code but this gives me an error about y. How should I implement the euler function correctly? And also I could not determine how I can draw the solution curves..
function E=euler(f,y)
%Input - f is the function entered as a string 'f'
% - a and b are the left and right endpoints
% - ya is the initial condition y(a)
% - M is the number of steps
%Output - E=[T' Y'] where T is the vector of abscissas and
% Y is the vector of ordinates
h=0.1;
y(0)=0;
for j=0:15
Y(j+1)=Y(j)+h*feval(4*(y(t)+1));
end
Patch:
h = 0.1;
y(1) = 0;
for j = 1:16
Y(j + 1) = Y(j) + h * feval(4 * (y(t - 1) + 1));
end
Well, I am not sure about the mathematical part, but - The indices need to start at "1". Other then e.g. in C, you must not use "0" as an index.