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 comentarios

Star Strider
Star Strider el 4 de Oct. de 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
Cam Salzberger el 4 de Oct. de 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
AMELA SHAHINI el 4 de Oct. de 2018
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?

Iniciar sesión para comentar.

Respuestas (1)

madhan ravi
madhan ravi el 5 de Oct. de 2018
Editada: madhan ravi el 5 de Oct. de 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.

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 4 de Oct. de 2018

Editada:

el 5 de Oct. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by