Matlab for loop and difference equation question

Click For Summary

Discussion Overview

The discussion centers around solving difference equations using MATLAB, specifically focusing on the implementation of for loops to compute values based on given recursive relationships. Participants are addressing coding issues and providing solutions related to indexing and initialization of variables.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant presents a difference equation and describes an error encountered when attempting to implement it in MATLAB.
  • Another participant suggests that MATLAB does not allow indexing at zero and recommends starting the loop at k=1 instead of k=0, while also noting the need to initialize y(0) correctly.
  • A further suggestion is made to use a temporary variable to hold the previous value of y to maintain the correct indexing for time.
  • A new difference equation is introduced by a different participant, seeking guidance on solving it in MATLAB with a unit step input.

Areas of Agreement / Disagreement

Participants generally agree on the need to adjust the indexing in MATLAB to avoid errors, but the discussion remains unresolved regarding the best approach to implement the second difference equation presented.

Contextual Notes

There are limitations related to the initialization of variables and the handling of array indices in MATLAB, which may affect the implementation of the difference equations discussed.

Who May Find This Useful

This discussion may be useful for individuals learning to implement difference equations in MATLAB, particularly those encountering similar indexing issues or seeking to understand recursive relationships in coding.

Ethers0n
Messages
27
Reaction score
0
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

Thanks for any help.
 
Physics news on Phys.org
Matlab doesn't like to store things in the 0th element of an array. Your loop starts at k=0, so it's trying to access y(0) which doesn't exist. You'll have to start at k=1 and just know that k=1 corresponds to t=0.

Also, it doesn't look like you have actually told it that y(0) = 0. You need to say y(1) = 0.

If you really want y(1) to be time = 1, you could do something like

yold = 0;

for k = 1:5
y(k) = -0.5*yold + x;
yold = y(k);
end

Then the time index will be correct, but your y values won't tell you that y(0) = 0.
 
Thanks.
That seemed to do the trick.

J
 
how to solve the following difference equation in matlab
y(n)=0.5y(n-1)+0.2y(n-2)+.78y(n-3)+x(n)+.2x(n-1)
my input is unit step
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K