# I don't get the correct output from my function

조회 수: 2 (최근 30일)
Thomas Hammer 2020년 2월 19일
댓글: Steven Lord 2020년 2월 19일
Hello community. I've struggled for a bit now with this.
I don't understand how I can get elementwise multiplication and addition to work here. The graph is not what it should look like when I run this program. I've tested with other software, such as Geogebra. Other than that, when I change the range from -5 to -10, the graph shifts with it.
I just don't see how to fix this problem. I would love help on this. Thanks.
values = -5:0.1:5;
f = @(x)(exp(1-2.*x).*(x+cos(2.*x)));
plot(values, f(values))
##### 댓글 수: 5이전 댓글 3개 표시이전 댓글 3개 숨기기
편집: Adam Danz 2020년 2월 19일
"I don't understand how I can get elementwise multiplication and addition to work here."
What's not working with your current code? The only addition I see is (x+cos(2.*x)) which is doing element-wise addition.
"The graph is not what it should look like"
What should it look like?
"when I change the range from -5 to -10, the graph shifts with it"
Do you mean -5 to +10? If the x values are changed, the range of data should shift horizontally according to the new x values.
Thomas Hammer 2020년 2월 19일
편집: Thomas Hammer 2020년 2월 19일
Thanks for the reply Adam. When plotting the values I would suppose that the function gives the same different outputs (y values), for the same inputs (x values). It doesn't seem like it's doing that. https://imgur.com/a/ZWAbEPa Here's an imgur of what I expect vs what I'm getting.
(I completely missed the fact that it said x 10^5 on the top!)

댓글을 달려면 로그인하십시오.

### 채택된 답변

Turlough Hughes 2020년 2월 19일
편집: Turlough Hughes 2020년 2월 19일
The function is correct. However, because this function outputs very large numbers depending on your range (in terms of absolute value), you end up with missing the details that you appear to be looking for.
Try the following:
f = @(x) exp(1-2*x).*(x+cos(2*x))
fplot(f,[-5 15])
ylim([-2 4])
##### 댓글 수: 2없음 표시없음 숨기기
Thomas Hammer 2020년 2월 19일
Ah! Thank you! I didn't know about this. I thought it would map the corresponding outputs to the screen, but I guess since the output has extremely low y values, I completely miss out on the detail. Thank you for understanding me, I'm still learning this application and how to use it.
Steven Lord 2020년 2월 19일
Note the limits of the axes in the geogebra diagram: the lower X limit is -1.5 or -2, not -5 or -15. If you do what Turlough Hughes did but use [-1.5 6] as the limits instead of [-5 15] it looks pretty similar to the geogebra diagram. If I tweak it a little more the resemblance is even stronger.
Let's enable the major and minor grids:
grid on
grid minor
make the axes rulers cross at the origin:
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
and have the tick locations match:
xticks(-1.5:0.5:6)
yticks(-1.5:0.5:3)
Adjust the size of the figure window with the mouse (dragging the sides or one of the lower corners) to the same size as the picture on imgur and they agree pretty well.

댓글을 달려면 로그인하십시오.

### 추가 답변 (1개)

M 2020년 2월 19일
When you change from -5 to -10, the absissa of the plots change, yes. But the value of the function does not change. What makes you think there is an issue here ?
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Thomas Hammer 2020년 2월 19일
This is what I mean. There is an error with my program.
The function I want to plot is (e^(1-2x))*(x+cos(2x)).

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by