I don't get the correct output from my function
Show older comments
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 Comments
Turlough Hughes
on 19 Feb 2020
The equation you are solving based on the code you provided is:

Is that what you intended?
Note that you dont need element-wise multiplication for a scalar times a vector. You can modify the above anonymous funtion to be:
f = @(x) exp(1-2*x).*(x+cos(2*x))
Alex Mcaulley
on 19 Feb 2020
What do you expect for your plot? (you can use fplot)
f = @(x)(exp(1-2.*x).*(x+cos(2.*x)));
fplot(f,[-5,10]))
Thomas Hammer
on 19 Feb 2020
"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
on 19 Feb 2020
Edited: Thomas Hammer
on 19 Feb 2020
Accepted Answer
More Answers (1)
M
on 19 Feb 2020
0 votes
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 Comment
Thomas Hammer
on 19 Feb 2020
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!