I don't get it... Help plotting this on MATLAB?

I can't seem to figure out how to solve this question. I've tried looking at an example and doing it but this has more variables and I'm just not getting the result I'm supposed to.
I've heard that using a for loop would work when incrementing and calculating each values, but it's just too confusing.
Files are attached... Any help would be very much appreciated.
Please and thank you!

2 comentarios

Konstantinos Sofos
Konstantinos Sofos el 21 de Mzo. de 2015
You could read the stuff here Signals & Systems - Fourier Series Example
Annalise
Annalise el 21 de Mzo. de 2015
I looked through, and made a few adjustments to my code, however, the plotting part looks a lot more complicated than it has to be...

Iniciar sesión para comentar.

 Respuesta aceptada

Giorgos Papakonstantinou
Giorgos Papakonstantinou el 21 de Mzo. de 2015
Editada: Giorgos Papakonstantinou el 21 de Mzo. de 2015
Annalise I will try to demonstrate how you may plot a piecewise function. In a similar manner you may plot yours.
Lets assume you have a vector:
x = -10:1:10;
And you have a piecewise function such as f(x) = |x|. This function is defined as:
  • f = |x|, x<0
  • f = 0, x=0
  • f = |x|, x>0
To define the three different intervals of x in Matlab you should this:
s1 = x<0;
s2 = x==0;
s3 = x>0;
To define the values of f in these intervals you should do this:
f(s1) = -x(s1);
f(s2) = 0;
f(s3) = x(s3);
To plot f with stem:
stem(x(s1), y(s1), 'r')
hold on
stem(x(s2), y(s2), 'g')
stem(x(s3), y(s3))
As you see a for loop is not necessary for a piecewise function which is defined on 3 intervals.

7 comentarios

Annalise
Annalise el 21 de Mzo. de 2015
Let me try it out and see. Thank you ^^
This worked perfectly and I'm getting the result I needed! The only issue I'm having right now is that one value, at 1, isn't displayed but is at zero. This is the code I've written:
n_0 = 1:4:20;
n_1 = 3:4:20;
n_2 = 0:2:20;
s1 = n_0;
s2 = n_2==0;
s3 = n_1;
f(s1) = 4./(pi*s1).*sin(s1*pi/2);
f(s2) = 0;
f(s3) = -4./(pi*s3).*sin(s3*pi/2);
stem(n_0, f(s1), 'r')
hold on
stem(n_1, f(s3), 'b')
Annalise, the n_0, n_1 and n_2 represent the x where f(x) is evaluated. In the simple example I gave you the x was a vector of integers from -10 to 10 with a step of 1. However, x could be from 0 to 1 with a step of 0.1 i.e.
x = 0:0.1:1;
or any other real number.
In your case n_0, n_1 and n_2 are integers but they could also be real numbers.
In my example s1 was a logical vector (look here and here to get a deeper insight). In your solution s1 is not a logical vector it is n_0. If n_0 was a vector of real numbers then s1 would also be a vector of real numbers. In this case Matlab would throw an error if you had issued f(s1) in the command:
>> n_0=0:0.1:1;
>> s1=n_0
>> f(s1)
Error: Subscript indices must either be real positive integers or logicals.
You did not apply correctly my example to your problem. Try to redefine the interval s1, s2, s3.
To give you a hint. Keep them as they are the n_0, n_1 and n_2.
x =0:1:20;
s1 = x==n_0
etc.
I think that you will manage to solve your problem.
I'm sorry.. Do you mean i should keep it like this?
n_0 = 1:4:20;
n_1 = 3:4:20;
n_2 = 0:2:20;
x = 0:1:20;
s1 = x==n_0;
s2 = x==0;
s3 = x==n_1;
I get an error though, "Error using == Matrix dimensions must agree."
Annalise
Annalise el 21 de Mzo. de 2015
This is the code I've written, like above, and when run it gives me the graph that I'm expecting. It's just that the value at 1, for C_n, is not the value that I want and is at zero. I'm new to MATLAB so I'm a little unsure when it comes to fixing up all the codes and such...
Annalise you have to review the concept of indexing. From your code I have the following remarks:
  • First of all there is no need to define s1 since it is equal with n_3.
>> n_3 = 1:4:20
n_3 =
1 5 9 13 17
s1 = n_3;
The same for s3 and n_1.
  • Secondly, you write:
f(s1) = 4./(pi*s1).*sin(s1*pi/2)
In this statement s1 is indexing f. s1 is equal with the vector 1 5 9 13 17. Therefore, you literally say that the 1st, 5th, 9th, 13th and 17th element of f are going to be equal with the formula that you provided. That is:
4./(pi*s1).*sin(s1*pi/2)
So since the first statement in your code is f(s1), Matlab at this point knows only the values of f(1), f(5), f(9), f(13) and f(17). It doesn't know yet all the others! Hence, it assigns to all other elements of f (meaning the elements 2, 3, 4, 6, 7, 8, 10, 11, 12, 14, 15, and 16) the value of 0. Try it your self. Execute the you code only until line 9. Look what you get:
f =
Columns 1 through 16
1.2732 0 0 0 0.2546 0 0 0 0.1415 0 0 0 0.0979 0 0 0
Column 17
0.0749
  • My biggest remark is when you define n_2 as logical:
n_2 = 0:2:20;
s2 = n_2==0
While s1 and s3 are integers, s2 is oddly logical. In my example they were ALL logicals. If you execute at the command prompt s2. Then:
s2 =
1 0 0 0 0 0 0 0 0 0 0
In this way, when you write at line 10:
f(s2) = 0
Matlab will return those elements of f for which s2 is true (or one). Notice that only the first value of s2 is true. All the other values are false (or zero). Thus, the f(s2) will point at the first element of f. You literally assign to the first element of f the value of zero . While previously the first of element of f was 1.2732. This is the reason why f(1) in your plot is actually zero. And that's why I would suggest you once again to read these two links that I sent you.
  • Additionally, you have to decide if you are going to work with logical or linear indexing. Choose one way and stick to it.
  • Two ways to deal your problem:
a)
%%Logical indexing
n_1 = 3:4:20;
n_2 = 0:2:20;
n_3 = 1:4:20;
x = 1:20; % x has 20 elements
s1=x==n_1; % s1 20 elements. Only elements 3, 7, 11, 15 and 19 are true. The others false
s2=x==n_2; % similarly to s1
s3=x==n_3; % similarly to s1
f(s1) = 4./(pi*x(s1)).*sin(x(s1)*pi/2);
% etc...
b)
%%No Indexing
n_1 = 3:4:20;
n_2 = 0:2:20;
n_3 = 1:4:20;
f1 = 4./(pi*n_1).*sin(n_1*pi/2)
f2 =...
f3 =...
Annalise
Annalise el 21 de Mzo. de 2015
Editada: Annalise el 21 de Mzo. de 2015
Thank you so much for all your help ^^
For the moment, I deleted the zero value one (f1) leaving the other two, so there are no logical values. It doesn't make a difference to the result so I'm happy with that, since submission is in a few hours and my is too frazzled up to think about this clearly.
I'll read through everything again, tomorrow, and try to fix the code up to the way it should be. Once again, thank you so very very much. It's much appreciated.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 21 de Mzo. de 2015

Editada:

el 21 de Mzo. de 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by