Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

need difference equation and for loop help

Status
Not open for further replies.

ethers0n

Electrical
Joined
Mar 30, 2005
Messages
1
Location
US
I'm attempting to solve a difference equation

y(k+1) = -0.5*y(k) + x(k)

where y(0) = 0 and x(k) is in my case a unit step function ie = 1

well, i'm trying to solve this using a for loop, but am having some trouble. The code i've generated gets an error, "Index into matrix is negative or zero."

Any ideas?
My code is below.

k = 0 ; %counter variable
y = 0; %y(k)
x = 1; %x(k), unit step function

for k = 0 : 5

y(k+1) = -.5*y(k) + x;

y(k) = y(k+1)

end
 
Well, the error message seems pretty informative.

If k is zero and you call y(k) what else would you expect?



Cheers

Greg Locock

Please see FAQ731-376 for tips on how to make the best use of Eng-Tips.
 
MATLAB matrix indices start at 1, not 0.

xnuke
"Do you think you used enough dynamite there, Butch?"
Please see FAQ731-376 for tips on how to make the best use of Eng-Tips.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top