Matlab for loop and difference equation question

In summary, the speaker is attempting to solve a difference equation using a for loop in Matlab. However, they are encountering an error and are seeking help with their code. The conversation also includes a clarification about storing values in the 0th element of an array and a suggestion for correcting the issue. Lastly, the speaker provides an example of a different difference equation and mentions using a unit step function as input.
  • #1
Ethers0n
27
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
  • #2
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.
 
  • #3
Thanks.
That seemed to do the trick.

J
 
  • #4
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
 

Related to Matlab for loop and difference equation question

1. What is a for loop in Matlab?

A for loop in Matlab is a programming construct that allows for repeated execution of a code block for a specified number of times. It is commonly used for tasks such as iterating through arrays or performing calculations on a set of data.

2. How do I write a for loop in Matlab?

To write a for loop in Matlab, you first need to define a variable that will act as the loop counter. Then, use the "for" keyword followed by the variable name, an equal sign, and the range of values for the loop. Finally, enclose the code block to be repeated in curly braces {}.

3. What is a difference equation in Matlab?

A difference equation in Matlab is a mathematical representation of a recursive relationship between a sequence of values. It is commonly used in time series analysis and signal processing to model the behavior of a system over time.

4. How do I implement a difference equation in Matlab?

To implement a difference equation in Matlab, you first need to define the initial conditions and parameters of the equation. Then, use a for loop to iterate through the desired number of time steps. Within the loop, use the previously calculated values to compute the next value in the sequence.

5. What are some common errors when using for loops and difference equations in Matlab?

Some common errors when using for loops and difference equations in Matlab include not properly defining the loop counter variable, not updating the loop counter within the loop, and not properly handling boundary conditions. It is also important to check for typos and incorrect syntax, as these can cause unexpected results.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
263
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
680
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
956
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
223
Back
Top