Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

learning basics of Matlab

Status
Not open for further replies.
Thank you, Dougy.

I assume that with the first code block you wish to plot 51 points each having a y value of 3?

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.

Regards
PG
 
Last edited:
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.
 
Last edited:
Thank you very much.

I'm sorry but Matlab is quite a wild beast. I have used C++ and it was really straightforward once you understand the basics.

I was experimenting with the code below but now I get another error of which I can't make any sense. Could you kindly help with this? Thanks.

Code:
x=[-5:0.001:10]; 
y=2*x*((x>0 & x<3) | (x>5 & x<9));
??? Error using ==> mtimes
Inner matrix dimensions must agree.
 
Last edited:
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.

The .* is the important bit
 
Once again, thanks a lot.

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);

Regards
PG
 
Last edited:
Hi

I'm so sorry. I have quoted the wrong code in my last post. The code which I should have quoted is shown below. I hope it's alright now.

Code:
x=[-10:10];
y=2*x;
stem(x,y);
 
Last edited:
So any hints regarding how it was different? Still using stem?
 
So any hints regarding how it was different? Still using stem?

I don't remember any of it. But I will tell you tomorrow or later. Then, we can discuss it. Thank you for being there to help me with this stuff.

Best wishes
PG
 
That would help, as there are many ways of doing any task.
 
That would help, as there are many ways of doing any task.

Hi Doug

The guy was doing something similar to this:

Code:
n = [-2:6];
y = [-4:2:12];
stem(n,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.

Thank you.

Regards
PG
 
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:
Code:
x = 1:5;
y = x .^ 2 - 1;
 
Hi again,

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.

Code:
% CODE #1
clc;
x=[-3:0.01:4];
y=1*ones(1,length(x)).*(x>=-1 & x<=0) & (y=-1*ones(1,length(x)).*(x>=0 & x<=1))
plot(x,y),axis([-3 4 -5 5]);

% CODE #2
clc;
x=[-3:0.01:4];
y=1*ones(1,length(x)).*(x>=-1 & x<=0);
z=-1*ones(1,length(x)).*(x>=0 & x<=1);
plot(x,y,'r',x,z,'r','linewidth',3),axis([-3 4 -5 5]),grid on;
 

Attachments

  • lab8_task2.jpg
    lab8_task2.jpg
    28.5 KB · Views: 153
Last edited:
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:
Code:
clc;
x=[-3:0.01:4];
y=1*(x>=-1 & x<=0) + -1*(x>=0 & x<=1);
plot(x,y,'r','linewidth',3),axis([-3 4 -5 5]),grid on;

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.
 
Hi

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.

Code:
syms x;
% P(x)=x^2+3*x+6; 
p=[1,3,6]; poly2str(p,'x')

PS: It's working now. I have saved the script with incorrect format. Just had to change the file name. Anyway, thanks.
 
Last edited:
What is LCS_working? Is it part of your code?

The code you posted runs fine.
 
Hi Doug

What is LCS_working? Is it part of your code?

The code you posted runs fine.

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].

Kindly help me with the queries. Thank you.

Regards
PG
 
Q1:
type: help tf2zp

Q2: Note that eval() is not needed, but shows how to get matlab to parse a string value
>> y = eval('-4*x^2 + 2*x + 1')
y =
- 4*x^2 + 2*x + 1

>> sym2poly(y)
ans =
-4 2 1
 
Thank you, Doug.

Matlab doesn't give much information on what "k" is. Anyway, I really don't need to know it.

Best regards
PG
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top