can't plot exp * sin graph
显示 更早的评论

i can't plot this expression.... i dont know why. please help
1 个评论
John D'Errico
2020-5-24
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.
采纳的回答
更多回答(1 个)
Shay
2020-5-24
0 个投票
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 个评论
John D'Errico
2020-5-24
编辑:John D'Errico
2020-5-24
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!