Need help graphing a step function within Matlab

In summary: Sets limits of xsteps = size(x,2); // I'm not exactly sure what these do, but I was told to do this becausey = zeros(steps); // I needed to define yif (x <= 2) y = x^2;elseif (x >= 2)y = 2.*x-8;plot(x,y)...logical function to test for equality.
  • #1
Lolsauce
23
0
So I'm not going to lie, I'm pretty new with this software. I was assign a homework assignment, but I'm not sure how you graph a step function.

Given this waveform use the step function to find an equation:

http://img163.imageshack.us/img163/2545/stepfunction.jpg"

I calculated the step function to be:

x(t) = (t^2)[u(t)-u(t-2)]+2(t-4)[u(t-2)-u(t-4)] , which I'm pretty positive is right.

Now graphing in Matlab is my biggest problem. I've looked over my textbook at sample graphing techniques, this is what I have attempted.

Code:
>> x = inline('(t^2)(u(t)-u(t-2))+2(t-4)(u(t-2)-u(t-4))', 't')

g =

     Inline function:
     g(t) = (t^2)(u(t)-u(t-2))+2(t-4)(u(t-2)-u(t-4))

>> t = (-1:5);
>> plot(t,g(t));
[COLOR="Red"]? Error using ==> inlineeval at 15
Error in inline expression ==>
(t^2)(u(t)-u(t-2))+2(t-4)(u(t-2)-u(t-4))
 Error: Unbalanced or unexpected
 parenthesis or bracket.

Error in ==> inline.subsref at 27
    INLINE_OUT_ =
    inlineeval(INLINE_INPUTS_,
    INLINE_OBJ_.inputExpr,
    INLINE_OBJ_.expr);[/COLOR]

Any tips or suggestions would be helpful, thank you.
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Matlab does not perform multiplication in general when one enters things such as a(b + c).

You need to specify the array product (.*) operator for multiplication, as you want the function evaluated for each value in the vector t, and do not wish to do matrix multiplication using the entire t vector.

There is no function u provided with MatLab. You may create one with the following code.

Code:
u = @(x)(x>=0);
 
  • #3
MisterX said:
Matlab does not perform multiplication in general when one enters things such as a(b + c).

You need to specify the array product (.*) operator for multiplication, as you want the function evaluated for each value in the vector t, and do not wish to do matrix multiplication using the entire t vector.

There is no function u provided with MatLab. You may create one with the following code.

Code:
u = @(x)(x>=0);

What do you mean by no function u? Do you mean I have no declared one? Shouldn't I enter this as u, since it is the unit step function?

Code:
>> u = inline('(t>=0)','t')
 
  • #4
Lolsauce said:
What do you mean by no function u? Do you mean I have no declared one?

I meant no function named "u" is included with MatLab.

Lolsauce said:
Shouldn't I enter this as u, since it is the unit step function?

You may name it that if you wish.
 
  • #5
MisterX said:
I meant no function named "u" is included with MatLab.
You may name it that if you wish.

Oohhhhh I got it, dumb mistake :(
So what I have now is:

Code:
>> u = inline('(t>=0)','t')       //this defines u also known as the step function
u = Inline function:             //this is returned by Matlab
     u(t) = t(>=0)
p = inline('(t>=0&(t<4))','t')  //this defines p and let's the t boundaries go from 0 to 4 just like the one in the problem
t = (-1:5);                           // sets t for the values from -1 to 5 just to be able to graph the graph
plot(t,p(t));                //plots the graph
xlabel('t'); ylabel('p(t) = (t.*t)(u(t)-u(t-2))+2.*(t-4).*(u(t-2)-u(t-4))     //sets the function
axis([-.1 5 -5 5]);      // sets the axis to be able to see the graph

But there is a problem after I put in:

Code:
xlabel('t'); ylabel('p(t) = (t.*t)(u(t)-u(t-2))+2.*(t-4).*(u(t-2)-u(t-4))
[COLOR="Red"]Error: A MATLAB string constant is not
terminated properly.[/COLOR]

Any ideas? :cry:
 
  • #6
Code:
xlabel('t'); ylabel('p(t) = (t.*t)(u(t)-u(t-2))+2.*(t-4).*(u(t-2)-u(t-4)[B][COLOR="Red"][SIZE="6"]'[/SIZE][/COLOR][/B])

Check your syntax.
 
  • #7
So I scraped all my old code and just started a new simple approach using an if statement. Although there is a problem When I graph it, all I get is a linear straight line.

Code:
x = -1:.01:5;         //Sets limits of x
steps = size(x,2);   // I'm not exactly sure what these do, but I was told to do this because
y = zeros(steps);   // I needed to define y
 if (x <= 2)            
y = x^2;
elseif (x >= 2)
y = 2.*x-8;
plot(x,y)

Any tips?
 
  • #8
The expression "x <= 2" evaluates to a "logical array" with the same dimensions as x. Each value in this "logical array" would correspond an element of x, and would indicate if that particular value was less than or equal to 2.

The situation is similar for the expression "x >= 2".According to the the http://www.mathworks.com/help/techdoc/ref/if.html", "An evaluated expression is true when the result is nonempty and contains all nonzero elements (logical or real numeric). Otherwise, the expression is false."

So the lines
Code:
y = x^2;
and
Code:
y = 2.*x-8;
would never be executed, because the "logical arrays" would not contain all nonzero elements. Even if they did, it would not achieve the desired result.

The following links may be worth looking at.

http://www.mathworks.com/help/techdoc/matlab_prog/f0-40063.html#f0-38145"

http://www.mathworks.com/help/techdoc/math/f1-85462.html#bq7egb6-1"

You can use a logical array produced with a relational operator such as "<=" to index an array. You can use this technique to assign different parts of "y" separately.
 
Last edited by a moderator:
  • #9
Lolsauce said:
Any tips?

1. Use a for loop to calculate the values individually.
2. Avoid the if statement by using relational operators inline.
 

1. How do I graph a step function in Matlab?

To graph a step function in Matlab, you can use the "plot" function and define the x and y values for each step. You can also use the "stairs" function to create a step-like graph.

2. Can I customize the appearance of the step function graph?

Yes, you can customize the appearance of the step function graph by changing the line color, thickness, and style using the "plot" or "stairs" function. You can also add labels, titles, and legends to make the graph more informative.

3. How do I create a step function with multiple steps?

To create a step function with multiple steps, you can define multiple x and y values for each step and plot them on the same graph. Alternatively, you can use the "hold on" command to plot each step separately on the same graph.

4. What if I want to change the position of the steps in the graph?

You can change the position of the steps in the graph by adjusting the x values for each step. You can also use the "shift" function to shift the entire graph horizontally or vertically.

5. How can I use a step function in my data analysis or modeling?

Step functions are commonly used in data analysis and modeling to represent sudden changes or events. They can be used to model real-world phenomena such as stock market fluctuations, population growth, or switch-like behavior in biological systems. You can use the graph to visualize the changes and analyze the data more accurately.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
937
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Calculus and Beyond Homework Help
Replies
8
Views
231
  • Calculus and Beyond Homework Help
Replies
1
Views
704
  • Differential Equations
Replies
1
Views
750
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top