How Do You Plot a Cosine Function with a Unit Step in MATLAB?

In summary, the conversation is about plotting the function cos(pi*n)*u(n) and how to include the unit step function in the plot. The steps involved include defining the values of n and y, using the unit step function to create x(n), and plotting the function using the plot function. It is also mentioned that x(t<=0)=0 is used to set the value of x to 0 for negative values of t, and that x=cos(pi()*t) is used to define the function.
  • #1
cleopatra
45
0

Homework Statement



plot this function:
cos(pi*n)*u(n)



The Attempt at a Solution



n=0:1:10;
y=(n>=0);

x(n) = cos(pi()*n)*y

figure(1)
plot(y)
 
Physics news on Phys.org
  • #2
cleopatra said:

Homework Statement



plot this function:
cos(pi*n)*u(n)

The Attempt at a Solution



n=0:1:10;
y=(n>=0);

x(n) = cos(pi()*n)*y

figure(1)
plot(y)

y(t>=0)=1; ?

This should work ..
t=-2:0.1:2;
x=cos(pi()*t); x(t<=0)=0;
plot(t,x);
 
Last edited:
  • #3
I don´t get what x(t<=0)=0 does ?
And where do you multiply the unit step function?
You wrote x=cos(pi()*t); but I can´t see where u(t) is...
 
  • #4
nevermind I got it. thanks
 

Related to How Do You Plot a Cosine Function with a Unit Step in MATLAB?

1. What is a unit step function and how is it represented in Matlab?

A unit step function, also known as the Heaviside step function, is a mathematical function that has a value of 0 for all negative inputs and a value of 1 for all positive inputs. In Matlab, it is represented as the "heaviside" function and is denoted by the symbol "u".

2. How do I plot a unit step function in Matlab?

To plot a unit step function in Matlab, you can use the "plot" function and specify the heaviside function as the input. For example, to plot a unit step function with a delay of 2, you can use the code "plot(t,heaviside(t-2))", where "t" is the time variable.

3. Can I use a unit step function in an if statement in Matlab?

Yes, you can use a unit step function in an if statement in Matlab by using the "heaviside" function as the condition. For example, the code "if heaviside(x-5)==1" will execute the following code if the input "x" is greater than or equal to 5.

4. How does the unit step function relate to the unit impulse function in Matlab?

The unit impulse function, also known as the Dirac delta function, is defined as the derivative of the unit step function. In Matlab, it is represented as the "dirac" function and is commonly used in signal processing and control systems. It can be thought of as a function that has an infinitely high value at a specific point and is 0 everywhere else.

5. Can I use the unit step function to solve differential equations in Matlab?

Yes, the unit step function can be used to solve differential equations in Matlab by defining it as a piecewise function and using it in conjunction with the "ode45" function, which is used to solve ordinary differential equations. With the help of the unit step function, you can model systems with discontinuities or sudden changes in behavior.

Similar threads

  • Calculus and Beyond Homework Help
Replies
10
Views
1K
  • Calculus and Beyond Homework Help
Replies
3
Views
352
  • Calculus and Beyond Homework Help
Replies
6
Views
430
  • Calculus and Beyond Homework Help
Replies
5
Views
655
  • Calculus and Beyond Homework Help
Replies
1
Views
729
  • Calculus and Beyond Homework Help
Replies
2
Views
1K
  • Calculus and Beyond Homework Help
Replies
6
Views
794
  • Calculus and Beyond Homework Help
Replies
16
Views
600
  • Calculus and Beyond Homework Help
Replies
2
Views
28
  • Calculus and Beyond Homework Help
Replies
1
Views
302
Back
Top