Why is my function giving me the wrong output ?
Show older comments
Matlab for me is a new program. I have to write and plot the function : fb (x) = xe^(-x+1) - sin(2x )+ x - 7/2, but my x goes from inf to inf. I have written it like this :
function [ y ] = fb ( x )
y = x.*exp(-x+1)-sin(2*x)+x-(7/2)
end
and I tried with this script :
x=-5:5;
y=fb(x)
plot(x,y,'r')
What is wrong with my way of thinking ? thank you
3 Comments
Star Strider
on 4 Oct 2018
Your script and function work correctly when I run them.
I do not understand ‘... my x goes from inf to inf.’
Please explain this. What do you want to do?
Cam Salzberger
on 4 Oct 2018
I don't see an issue. When I run your code, I get this:

Did you have a figure open already, with axes in an unusual location? Try opening a new figure before plotting?
AMELA SHAHINI
on 4 Oct 2018
Answers (1)
madhan ravi
on 5 Oct 2018
Edited: madhan ravi
on 5 Oct 2018
syms x
y = x.*exp(-x+1)-sin(2*x)+x-(7/2)
fplot(y)
grid on
x=vpasolve(y==0,x)
Note: Make sure you have symbolic toolbox.
Remark: fplot (symbolic) command executes from -inf to inf.
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!