Why is my function giving me the wrong output ?

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

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?
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?
If I calculate my function online , the figure it's different. The x approximates to infinity, but in my function it goes to zero. The professor wants us to find the roots for this, so when y=o. but the answer is never right?

Sign in to comment.

Answers (1)

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

Asked:

on 4 Oct 2018

Edited:

on 5 Oct 2018

Community Treasure Hunt

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

Start Hunting!