Graph of function not match y(variable)

I wrote a non-polynomial function as y and made a plot graph from it but when I try to find a value of x from the function (simply using y(x)) it never seems to match what the value would be from the graph? For instance I can see that when x = 10 then y=-100 from the graph, but using y(10) I get the value x = -188.9. Relevant graph:
and y(10):
relevant code:
a5 = -2 * 10 ^ -5; %assign values of the equation variables
a4 = 3 * 10^ -5;
a3 = 2 * 10^ -2;
a0 = -50;
b1 = -10^ 2;
b2 = 10;
c1 = 2 * 10^ -1;
c2 = 10^ -1;
x= -37:37; %define the range of x for the graph
y = (a5.*x.^5 + a4.*x.^4 + a3.*x.^3 + a0 + b1.*(sin(c1.*x)) + b2.*exp(c2.*x)); %assign the y function
plot(x,y); %plot the graph
xlabel ('X axis'); %add the title, axis and grid to the graph
ylabel ('Y axis');
title ('Figure 1 - Function y as a function of x');
grid on;
end

 Respuesta aceptada

Michelangelo Ricciulli
Michelangelo Ricciulli el 15 de Mzo. de 2018

0 votos

Hi Gabriel, you are mixing up the indexes of the vector and the argument of a mathematical function. y(10) means the 10th value memorized in the array y. y is made of 37*2+1=75 values and you associated the 1st with x=-37, the 2nd with x=-36 ... So y(10) in matlab, is equivalent to y(-37+10)=y(-27). And if you look at your plot, the value in x=-27 is -188.9 .

5 comentarios

Gabriel Boiling
Gabriel Boiling el 15 de Mzo. de 2018
Ok then thank you for the explanation. Does that mean if I aim to use the bisection method for finding the root between x=10 and x=20 I should assign them as xu=10+37 and xl=20+37?
Gabriel Boiling
Gabriel Boiling el 15 de Mzo. de 2018
and also then take 37 away from my root
Easier would be to define an anonymous function and call it to evaluate your mathematical function.
f = @(x) (a5.*x.^5 + a4.*x.^4 + a3.*x.^3 + a0 + b1.*(sin(c1.*x)) + b2.*exp(c2.*x));
y = f(x);
The variable f is an anonymous function and y is the result of evaluating the anonymous function with the data stored in the variable x as input.
Michelangelo Ricciulli
Michelangelo Ricciulli el 15 de Mzo. de 2018
Editada: Michelangelo Ricciulli el 15 de Mzo. de 2018
yes, it is right... But I guess that if you are going to use bisection method, you are also going to change the spacing between x values (now they are spaced by 1, so, for example, you can't evaluate y in 10.33). Then you should modify accordingly the values xu and xl. In the end, is far more convenient to use the method proposed by Steven Lord
Gabriel Boiling
Gabriel Boiling el 15 de Mzo. de 2018
Ok then, thank you for all the help

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Animation en Centro de ayuda y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by