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

  • Thread starter Thread starter sloan13
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a MATLAB function intended to plot a polynomial equation, specifically examining the use of the fplot function and the appropriate range for plotting. The context includes homework-related queries and coding practices in MATLAB.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant presents a function intended to plot the equation y = x^4 - 4x^3 - 6x^2 + 15 but encounters an error when using fplot.
  • Another participant questions why the function returns a vector and suggests that fplot requires a different approach to defining the function.
  • There is a discussion about the range of values for plotting, with one participant noting that the problem specifies a range from -3 to 6, while another participant initially used 0 to 10000.
  • Participants discuss the implications of using a for loop to generate values for plotting and the necessity of defining the function correctly for fplot to work.
  • Advice is given regarding polynomial coding practices to avoid rounding errors, suggesting a more stable method for evaluating polynomials.

Areas of Agreement / Disagreement

Participants express differing views on the correct approach to defining the function for fplot, the appropriate range for plotting, and the necessity of returning a vector. The discussion remains unresolved regarding the best practices for using fplot versus plot.

Contextual Notes

There are limitations in the understanding of how to properly use fplot and the implications of the specified range in the homework problem. Some assumptions about the function's output and the plotting method are not fully clarified.

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

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K