The Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.matlab

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Ed Nowak

Posts: 8
From: Southern California
Registered: 3/3/13
Re: difference equation
Posted: Mar 3, 2013 4:10 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

> "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?



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2017. All Rights Reserved.