how to write a function in interval?

hi,
can you please help me to write this function code in matlab?
thank you in advance

2 comentarios

José-Luis
José-Luis el 21 de Oct. de 2014
Editada: José-Luis el 21 de Oct. de 2014
What have you tried so far?
I would recommend you to read the "Getting started" part of the documentation.
You might want to look at the fzero() function.
doc fzero
doc plot
doc fplot
lina
lina el 21 de Oct. de 2014
I attached the question in jpg form can't write the code :'(

Iniciar sesión para comentar.

 Respuesta aceptada

Matt Tearle
Matt Tearle el 21 de Oct. de 2014

0 votos

MATLAB Academy can help you learn the basics of MATLAB. Also the Getting Started section of the doc, as Jose-Luis suggested.
For this specific question, you'll need to:
  1. create a vector of x values from -2 to 5.
  2. create a corresponding vector of y values by evaluating the function.
  3. plot y as a function of x.
  4. maybe pretty up the graph a bit.
For 2 you'll need to know about array operations. For 4 you can use xlabel, ylabel, title, xlim, ylim, and/or axis.
As Jose-Luis mentioned, if you need the roots exactly, you can use fzero, but that seems a bit advanced for HW#1. Because the function is a polynomial, you could also use polyval (for part 2) and roots. But, again, that seems more than is necessary.

5 comentarios

lina
lina el 21 de Oct. de 2014
thank you Matt it works, but why i get error in mpower ? this is what I start with
X=[-2:5]
x=X'
the_function =((x^4)-(5.*x^3)+(2.*x^2)+(6.*x)-(4));
Matt Tearle
Matt Tearle el 21 de Oct. de 2014
Array operations! You've done 5.*x, which is fine, but actually not needed because 5 is a scalar (so 5*x is the same as 5.*x). However, x is a vector, which means x^2 = x*x is a problem. That exponentiation is interpreted in a matrix sense, so that's where you need your array operator ( .* or .^).
[And because I can't help myself, the square brackets around -2:5 aren't necessary. The brackets are for concatenating values into an array, but the : already makes an array. x = (-2:5)'; will get you the vector a little quicker and cleaner.]
lina
lina el 21 de Oct. de 2014
Thank u very much u r awesome! It works
lina
lina el 21 de Oct. de 2014
pardon Matt but there is another problem in plotting dimension
x=(-2:5);
y=x.^4-5.*(x.^3)+2.*(x.^2)+6.*(x)-4;
plot(y,[-2,5]);
I get this error ??? Error using ==> plot Vectors must be the same lengths.
what should I do?
thank u in advance
Matt Tearle
Matt Tearle el 22 de Oct. de 2014
You don't need to specify the domain to the plot function, just the x values and their corresponding y values (in that order): plot(x,y)
If you want to adjust the axis limits that MATLAB chooses, use the xlim and/or ylim functions (use the doc to see how they work).

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 21 de Oct. de 2014

Respondida:

el 8 de Feb. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by