Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: difference equation
Replies: 4   Last Post: Mar 5, 2013 3:03 AM

 Messages: [ Previous | Next ]
 Ed Nowak Posts: 8 From: Southern California Registered: 3/3/13
Re: difference equation
Posted: Mar 3, 2013 4:10 AM

> "vikas kumar" wrote in message
> <icqlsr\$7mp\$1@fred.mathworks.com>...

> > how do we solve this difference equation :
> > y(n) + 1/2y(n-1) + 3/4y(n-2) = x(n)
> >
> > where x(n)=(1/4)^n for all n>0
> > & y(-1)=4 , y(-2)=6

>
> use this program
> % This Matlab program implements the general
> difference equation,
>
> % a0y(n) = b0x(n) + b1x(n-1) + b2x(n-2) + ...+ bM
> x(n-M)
> % -a1y(n-1) - a2y(n-2) - ...- aN y(n-N)
>
> disp(' Implementation of general difference
> equation'); disp(' ');
> b = input('Enter the x coefficients b = ');
> a = input('Enter the y coefficients a = ');
> x1 = input('enter the input samples x = ');
> int = input('enter the intial values int = ');
> y= int;
> lint=length(int);
> x=[zeros(1,lint) x1];
> n=length(x);
> lb=length(b);
> la=length(a);
> for s= (lint+1):length(x)
> sumx=0;
> sumy=0;
> for j=2:(la)
> sumy=sumy+a(j)*y(s-j+1);
> end
> for f=1:lb
> sumx=sumx+b(f)*x(s-f+1);
> end
> y(s)=sumx-sumy;
> end
>
> disp('The output sequence is y(n) = ');
> disp(y);
> stem(y);
> xlabel('samples n');
> ylabel('y(n)');
> grid on;
> title('Implementation of difference equation');
>
>
> put y coefficient as a vector from the left side and
> x coefficient as a vector too from the right side
> and define the input vector on how many steps you
> want it before running the program so as to use it
> in input vector
> put the initial values y(-2) and y(-1)
> and you can see the solution

Hi Ali,
I responded to your program (I named it "differenceEqSolve.m") as follows:

Implementation of general difference equation

Enter the x coefficients b = {1}
Enter the y coefficients a = { 1 0.5 0.75}
enter the input samples x = 10
enter the initial values int = {0 4 6}

and I get the following error message:

Undefined function 'mtimes' for input arguments of type 'cell'.
Error in differenceEqSolve (line 21)
sumy=sumy+a(j)*y(s-j+1);

What have I done wrong?

Date Subject Author
11/27/10 vikas kumar
11/27/10 Nasser Abbasi
1/28/12 ali wathab
3/3/13 Ed Nowak
3/5/13 Ed Nowak