can't plot exp * sin graph
19 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
i can't plot this expression.... i dont know why. please help
1 Commento
John D'Errico
il 24 Mag 2020
Please, when you post a question, don't post a picture of your code. That forced me to type it in all over again. Had you pasted in simple text, I could just have done a copy and paste of the text myself, and made it easier to answer your question. Always make it easy for someone to help you. That get you the help you want faster and more efficiently.
Risposta accettata
John D'Errico
il 24 Mag 2020
Modificato: John D'Errico
il 24 Mag 2020
Your problem lies in not understanding the difference between the .* and * operators. (In fact, this was probably why you were assigned this homework problem in the first place, to teach you about the need for the .* operator.)
MATLAB is a matrix language, designed to work with matrix multiplication and linear algebra. The * operator does that. Much of the time you are just doing simple scalar products, so things like 2*3 work perfectly well. You don't realize, or don't care that you are using a tool for matrix operations. As well, you can even multiply all the elements of a matrix or vector by a constant, again * works. So 2*ones(2,2) works just fine in MATLAB, doing what you expect.
The problem arises when you want to multiply two vectors together, when you want to form the product of all elements of each vector, one at a time, resulting in a new vector of the same length. This element-wise operation is NOT done by the * operator. MATLAB provides different operators to perform element-wise multiplication, division, and power operators. They are the .* ./ and .^ operators.
You have exactly that case. I'll write it properly here:
t = 0:pi/100:20;
x = 10.05*exp(-0.2*t).*sin(1.99*t + 1.47);
plot(t,x)
And this will now work, producing a sine wave with an exponentially decreasing amplitude.
Note that the only difference in my code was the .* operator between the exp and sin terms, because there we wanted to form the product of two sets of vector elements to form a new vector. All of the cases where I just multiplied by a scalar constant were ok using the * operator, although it is not a bad idea to get used to using it always on such problems.
I need to point out that you can add or subtract two vectors together and get an element-wise result with no need for a special operator. So +(plus) and -(minus) always work as long as the vectors are the same size and shape. For example:
a = [1 2 3];
b = [2 3 5];
a + b
ans =
3 5 8
The only thing I will add is that when you build a vector using the colon operator as you did
t = 0:pi/100:20;
the final point in that vector need not be exactly 20.
t(end)
ans =
19.9805292768311
That is because the increment is pi/100, which does divide (20-0) evenly.
A better tool to generate such vectors, where the final point will be as you desire it, is linspace. The vector you generated had 637 elements in it. If you wanted to generate a vector of some number of elements with the desired start and end points, you can do this:
t = linspace(0,20,637);
t(end)
ans =
20
The spacing in the linspace vector is close to pi/100, although not exactly so, as again, you cannot use exactly that spacing and end at exactly 20.
t(2) - t(1)
ans =
0.031447
pi/100
ans =
0.031416
As I said though, your real problem was in not using the .* operator. Don't forget about the ./ and .^ operators too.
3 Commenti
John D'Errico
il 24 Mag 2020
Modificato: John D'Errico
il 24 Mag 2020
As I said, this is "how" they teach you that. ;-)
They give you a problem to solve where the * operator creates a failure. Frustrating, I know. But now you WILL remember to use .* so I guess your professor will have been successful. I'm sorry about the two day long headache.
Beware. Your NEXT assignment might be to teach you to use the ./ and .^ operators. Or perhaps your professor might have pity on you all and explain the difference in class. For example:
>> a = 1:3
a =
1 2 3
>> a^2
Error using ^ (line 51)
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To perform
elementwise matrix powers, use '.^'.
>> a.^2
ans =
1 4 9
The second case worked. You need to use .^ there to square each element, since that is an element-wise operation. In the next example, see what happens with division.
>> a = 1:3;
>> b = [2 3 5];
>> a/b
ans =
0.60526
>> a./b
ans =
0.5 0.66667 0.6
This last one was the tricky case, since MATLAB does not give you an overt error. It does something you will not expect though. If you really want the element-wise divide, use the ./ operator.
Do NOT forget to use ./ in your element-wise operations. As if not, I predict you will have a problem in the future. And then you will spend another day or so trying to figure out why code that looks right fails for no obvious reason.
Don't feel bad. We've all gone through it.
Più risposte (1)
Shay
il 24 Mag 2020
I suspect the way you have chosen your time value t assignment as incorrect as you have classified t over two ranges that is 0:pi and then divided by 100: 20. I tried the similar assignment to a simple function:
t= 1:7/1:3;
y = 2*t
-------------------
y =
2
Which seems to output only one value of the initial t value ranges of value one. However, specifying t over one range only produces:
t= 1:7
y = 2*t
-------------------
y =
2 4 6 8 10 12 14
Try specifying t over one range only, the plot should be generated.
2 Commenti
John D'Errico
il 24 Mag 2020
Modificato: John D'Errico
il 24 Mag 2020
Actually, there is no theoretical problem with creating t as was done. You should test the code to understand that.to be true.
The increment in the vector
t = 0:pi/100:20;
is less than the difference between the end points of the vector.
pi/100
ans =
0.031416
The increment is indeed a small number. So colon steps along at the desired spacing, going from 0 until the last element does not exceed 20.
numel(t)
ans =
637
t(end)
ans =
19.9805292768311
So there are 637 elements in that vector, the last of which is just a wee bit under 20. It worked with no problem.
What happens in the example you chose is 7/1=7 is GREATER than the difference between the start and end points.
t= 1:7/1:3
t =
1
t starts at 1, when MATLAB tries to add 7 to 1, sees that 8 would be greater than 3, and stops immediately. You get a vector of length 1.
The point is, you need to understand that the : operator has lower precedence in the order of operation than addition, subtraction, multiplication, and division, thus the simple arithmetic operations. You understand that normally to recognize, for example
1 + 2*3
ans =
7
yields 7. That is because addition and subtraction have a lower precedence. They are performd AFTER multiplication in such an expression. MATLAB effectively parses that expression as
1 + (2*3)
doing the multiplication first, THEN the addition.
But colon has an even LOWER precedence. For example I could write this:
1+2:2/4:3*2
ans =
3 3.5 4 4.5 5 5.5 6
MATLAB interprets this as effectively
(1+2):(2/4):(3*2)
So it performs the simple arithmetic operations FIRST, and then does the colon operation.
The only thing you need to worry about, is that there is room between the start and end points to stuff elements in there with the indicated spacing. The complete rules for precedence order for the operators in MATLAB are given here:
As you go further down that list, the precedence decreases with the lower order operators performed after those above them.
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!