Using fzero with syms or finding an alternative to syms
35 ビュー (過去 30 日間)
I need to first create an array for -10<x<10 with an interval of 0.5, then substitute each value of x into an equation (as stated below).
Next i need to plot the variables against eachother on a graph with a few constraints and finally i need to use fzero in order to find the root of the equation.
My code is as follows:
x = [-10:0.5:10];
for y= x.^3 - 3*x.^2 + 5*x*sin(pi*(x/4) - 5*(pi/4)) + 3
Fx = vpa(subs(y,x,-10:0.5:10),4)
x0 = 0;
rootx = fzero(Fx, x0)
fplot(y,[-10,10]), title('Graph of y= x^3 - 3*x^2 + 5*x*sin(pi*(x/4) - 5*(pi/4)) + 3 in the interval -10 < x < 10'), xlabel('-10 < x < 10'), ylabel('y values'), grid on, grid minor
In looking through this forum it is clear fzero is not compatible with the symbolic approach I've taken, so could someone please give me a hand with how to approach the above question without the use of syms.
The task is itself as follows:
Create an array for the x variable from x = −10 to x = 10, with appropriate spacing. Then use a for loop to evaluate the values of F according to equation (1).
(b) Plot F against x, using a title, labels and a grid.
(c) From the plot of F(x), estimate the three roots of equation (1). Use your estimates to find each root accurately with the command fzero. Then using hold on, plot the three roots on the same plot of point (b), with diamond markers. Add a legend to identify the function and the zeroes
Equation (1) is F(x)= x^3 - 3*x^2 + 5*x*sin(pi*(x/4) - 5*(pi/4)) + 3
It won't change much, but here is the error:
Error using fzero (line 181)
If FUN is a MATLAB object, it must have an feval method.
Error in CourseworkFileGoodStart (line 8)
rootx = fzero(Fx, x0)
Any and all help appreciated, my main issue is the first part of the question, using a for loop to evaluate (1) using the array [-10:10]. From their i will be able to proceed using existing questions on this forum.
Star Strider 2018 年 1 月 5 日
For this, use Anonymous Functions (link). Using the Symbolic Math Toolbox for this will only cause you problems.
Specifically, your ‘y’ equation then becomes:
y = @(x) x.^3 - 3*x.^2 + 5*x*sin(pi*(x/4) - 5*(pi/4)) + 3;
I leave the rest to you.