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

  • Thread starter sloan13
  • Start date
  • Tags
    Matlab
In summary, the problem asks for a function that will plot x values from -3 to 6, and returns a vector of y values corresponding to each x value. The problem also asks for a function that will plot x values from 1 to 10, and returns a vector of y values corresponding to each x value.
  • #1
sloan13
73
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
  • #2
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.)
 
  • #3
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.
 
  • #4
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.
 
  • #5
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
 
  • #6
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.
 

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

1. Why is my fplot not showing any graph?

There could be a few reasons for this. First, make sure that you have properly defined your function and included all necessary variables. Also, check that you have set the correct range for the x-axis using the "xlim" function. Finally, ensure that you have called the "fplot" function and not just defined it.

2. How do I change the color or style of my fplot?

To change the color of your fplot, you can use the "color" parameter within the "fplot" function. For example, "fplot(@(x) x^2, [-5,5], 'color', 'r')" will plot the function in red. To change the style, you can use the "linestyle" parameter. For example, "fplot(@(x) x^2, [-5,5], 'linestyle', '--')" will plot the function with a dashed line.

3. Why is my fplot distorted or not showing the full function?

This could be due to the range of the x-axis. Make sure that the range you have set for the x-axis is appropriate for the function you are plotting. If the function has a large range of values, you may need to adjust the "xlim" function accordingly. You can also try using the "ylim" function to adjust the y-axis range if needed.

4. Can I plot multiple functions on the same graph using fplot?

Yes, you can plot multiple functions on the same graph by using the "hold on" command before each "fplot" function. This will allow you to plot multiple functions on the same axes. Just make sure to use the "hold off" command when you are finished plotting to return to the default behavior of overwriting the previous plot.

5. Is there a way to label my fplot axes and add a title?

Yes, you can add labels to your fplot axes by using the "xlabel" and "ylabel" functions. For example, "xlabel('x-axis')" will label the x-axis as "x-axis". Similarly, you can add a title to your fplot by using the "title" function. For example, "title('My Function')" will add the title "My Function" to your plot.

Similar threads

Replies
3
Views
1K
Replies
1
Views
1K
Replies
10
Views
2K
Replies
13
Views
2K
Replies
1
Views
1K
Replies
3
Views
2K
Back
Top