Yes, it was exactly what I was trying to do. But I had only 50 points in mind!
That day the instructor gave us with **broken link removed** graph and asked us to write the code. I realized it was a discrete-time version of a line, and therefore I actually transformed the continuous-time line equation into a discrete version using the stem command.
1: Could you please tell me how I can plot the impulse function and time-shifted impulse function?
2: How do I use logical operators (<, >, >=, <=) in function definitions? Suppose I have a function y=2x for x >=0, and y=0 for x>10. How do I write the code for such a function and how do I plot it? Kindly help me with it.
I have two books but I couldn't find the information in any of them.
The stem command doesn't do any conversions, it just displays the data differently to a standard line plot.
1: The impulse function (delta) is simply
Code:
y = x == 0;
For the time-shifting, you can use:
Code:
y = (x - tau) == 0;
or shift and scale:
Code:
y = (gamma*x + tau) == 0;
NB if none of the discrete resulting scaled/shifted values actually fall exactly on zero, then you won't see a response (i.e. the impulse will be between samples and therefore missed).
2: You can write the function as a separate and use if-then-else while iterating through all elements in the input, or you can specify it in-line as before. Your example could become
Code:
y = 2*x.*(x>=0 && x<=10)
So to display it:
Code:
x=-20:.01:20;y = 2*x.*(x>=0 & x<=10);plot(x,y)
Or using stem:
Code:
x=-20:20;y = 2*x.*(x>=0 & x<=10);stem(x,y)
All of the comparisons of a vector return a "logical vector", i.e. a vector containing elements that are either 1 or 0. The element will be 1 where the comparison is true, and 0 otherwise. That's why e.g. the above impulse function generates a single 1 when the x is 0.
You need to use the element wise multiplication, other wise matrix multiplication is assumed . Therefore change time start of the line to: y = 2 * x .* (( etc.
That day when the instructor gave us this **broken link removed** and asked to write the code. As I mentioned previously I used the code given below. But later the instructor used a different code. I can't remember it because it happened more than two weeks ago and I have no record of that lecture. Can you guess any other way to draw the same plot? Kindly let me know if possible. Much obliged.
Code:
x =[0:50];
y = 3 * ones(1, length(x));
stem(x, y);
I have noticed that in many of the problems about plotting of discrete functions he defines values for "n" and "y" individually. I don't like his approach but it's easy.
Yeah, I wouldn't have guessed that he was explicitly stating each coordinate pair. Matlab can do that for you as shown above, and it's easier to see what the actual function is.
Why write:
Code:
x = 1:5;
y = [0 3 8 15 24];
when it's much more obvious what the relationship between x & y is if written thus:
The code #1 isn't working. The code #2 works but isn't perfect as is evident from the plot. I'm sure you can see what I'm trying to do. Could you please tell me how I can do it in simple way? Thank you for the help.
In the first piece of code you have a stray "y=" halfway along the assignment of y.
I assume you want the displayed output to be the same as your attached image but without the apparent continuous line along y=0. Your code could be adapted to the following:
The expression: "x > 2" will result in a vector as long as x, but the entries will be 0 where the corresponding x(i) value is <= 2 and 1 where x(i) > 2. So e.g. if x = 1:5, then x > 2 will be [0 0 1 1 1]. Therefore you don't need to multiply elementwise by ones(1, length(x)), which actually will have no effect.
I believe the script below is fine but Matlab still gives me an error: ??? Undefined function or variable 'LCS_working'. What is wrong with it? I have control systems, symbolic math and signal processing toolboxes installed. Please help me. Thanks.
I have fixed it. Actually "LCS_working" was just a part of not-allowed file name.
Q1: I was using the function [z,p,K]=tf2zp(Ns,Ds) to find zeros and poles, where "K" is said to be gain. What is this "gain"?
Q2: poly2str([-4 2 1], 'x') will generate output {-4x^2 + 2x + 1}. Is there any command which can do the reverse? I mean if I input {-4x^2 + 2x + 1} it give me [-4 2 1].