MatLab fplot help. Something may be wrong with my fplot?

  • Thread starter Thread starter sloan13
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
The user is attempting to plot the equation y = x^4 - 4x^3 - 6x^2 + 15 in MATLAB using fplot but encounters an error due to incorrect function definition and plotting range. The function created returns a vector instead of a valid function for fplot, which requires a function handle. Additionally, the user mistakenly plots from 0 to 10000 instead of the specified range of -3 to 6. It's advised to define the function correctly and use fplot without a for loop, or alternatively, create a vector of x values and use the plot function. The discussion emphasizes the importance of adhering to the problem's specified range and proper function syntax in MATLAB.
sloan13
Messages
71
Reaction score
0

Homework Statement



The problem was "Plot the following equation with MatLab, for values of x from -3 to 6.


Homework Equations



y = x^4 - 4x^3 - 6x^2 + 15

The Attempt at a Solution



I created a function.

function r = graph(x)
for i = 1:x
r(i) = i^4 - 4*i^3 - 6*i^2 + 15;
end


Then I passed fplot(graph(10), [0 10000]) through the command prompt.

It returned

Error using fcnchk (line 107)
FUN must be a function, a valid string expression,
or an inline function object.

Error in fplot (line 61)
fun = fcnchk(fun);


Any ideas on what this means?
 
Physics news on Phys.org
sloan13 said:
Code:
function r = graph(x)
for i = 1:x
r(i) = i^4 - 4*i^3 - 6*i^2 + 15;
end
Why does your function return a vector?

sloan13 said:
Then I passed
Code:
fplot(graph(10), [0 10000])
through the command prompt.
Why are you plotting from 0 to 10000? (And there is a comma missing.)

(Note: please use the CODE tags to delimit code.)
 
DrClaude said:
Why does your function return a vector?

I needed a vector to plot it, right?

Why are you plotting from 0 to 10000? (And there is a comma missing.)

they were just the first two numbers I thought of.
 
sloan13 said:
I needed a vector to plot it, right?
Not if you are using "fplot". You need to define a function, and "fplot" will call it as needed to generate to plot.

Alternatively, you can define a vector of x values, then build a vector of the corresponding y values, and use "plot" to plot it.

Think a bit about what you did in the function. Given a value of x, you generate a vector that contains the values of ##y(x)## for ##x## the integers between 1 and x.

I'll also give you some advice about coding polynomials. You should never write
Code:
r(x) = a4*x^4 +a3*x^3 + a2*x^2 + a1*x + a0;
as this can lead to a big rounding error (depending on the value of x and the coefficients of the polynomial). You should use instead
Code:
r(x) = (((a4 * x + a3) * x + a2) * x  + a1) * x + a0;

sloan13 said:
they were just the first two numbers I thought of.
But the problem tells you what the range of the plot should be.
 
DrClaude said:
But the problem tells you what the range of the plot should be.

I thought that meant to use a for loop to use each of those values. That's why I used for i = 1:10
 
sloan13 said:
I thought that meant to use a for loop to use each of those values. That's why I used for i = 1:10
That would have given you the values for ##x \in [1,10]##, not ##x \in [-3,6]## as the problem asks.
 

Similar threads

Back
Top