필터 지우기
필터 지우기

Using fzero with syms or finding an alternative to syms

조회 수: 24 (최근 30일)
Liam Wiltshire
Liam Wiltshire 2018년 1월 5일
댓글: Walter Roberson 2024년 5월 8일
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];
syms x
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
end
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.
Thank you

채택된 답변

Star Strider
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.
  댓글 수: 6
Miguel D. Rothe
Miguel D. Rothe 2020년 9월 27일
Beginner here: Not sure if it's good forum etiquette to revive a thread with tangent like this, but I ran into a similar issue. The main conflict is that the equation for which i am seeking to find the root is the derivative of another function. One cannot, to my knowledge, differentiate an anonymous function, but one cannot use fzero on a symbolic function. I used the simple workaround of matlabFunction(), but is there a way to do it without switching formats?
%assign constants to L,I,E,w...
syms x
def = ((w*x^2)/(48*E*I))*(3*L^2-5*L*x+2*x^2);
derivDef = diff(def);
%plot symbolic function...
%then find root
derivDef = matlabFunction(derivDef);
optimum = fzero(derivDef,L/2);
Seeing as this was an intro-level assignment, and we all know that in real life one is more likely to use fminbnd() or something similar on the original function than use fzero() on the derivative, perhaps this is a moot point. But would be interested to know.
Walter Roberson
Walter Roberson 2020년 9월 27일
you can vpasolve() instead of fzero()

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Enkhtsetseg
Enkhtsetseg 2024년 5월 7일
syms x;
% Define the equation f(x) = 0
f = x^3 - 6*x^2 + 11*x - 6;
% Find roots of f_sym using fzero
S_guess = 0; % Initial guess for S
eqn = @(S) subs(f, x, S); % Define the equation to solve
S = fzero(eqn, S_guess);
% Calculate A and B
A = S + 0.5;
B = S - 0.5;
% Display the solutions
disp('S:');
disp(S);
disp('A:');
disp(A);
disp('B:');
disp(B);
% Define the interval [a, b]
a = A; % A-г а-д орлуулах
b = B; % B-г б-д орлуулах
% Define the function f(x)
f = @(x) x^3 - 6*x^2 + 11*x - 6;
% Tolerance for the iterative method
tol = 1e-3;
results = [];
% Calculate the values of f(a) and f(b)
fa = f(a);
fb = f(b);
% Calculate the initial value of gamma
gamma = (a + b) / 2;
fgamma = f(gamma);
% Calculate the difference between b and a
b_minus_a = b - a;
% Perform the iterative method until the difference is less than the tolerance
while abs(b_minus_a) > tol
% Update the interval based on the sign of f(gamma)
if fgamma < 0
a = gamma;
fa = fgamma; % Update f(a) with f(gamma)
else
b = gamma;
fb = fgamma; % Update f(b) with f(gamma)
end
% Update the value of gamma
gamma = (a + b) / 2;
% Calculate the value of f(gamma)
fgamma = f(gamma);
% Update the difference between b and a
b_minus_a = b - a;
% Store the results for each iteration
results = [results; a, b, fa, fb, gamma, fgamma, b_minus_a];
end
% Display the results of the iterative method
disp(' a b f(a) f(b) gamma f(gamma) b − a ')
disp(results);
% Display the final result
disp('Final result:');
disp(['b − a: ', num2str(b_minus_a)]);
  댓글 수: 1
Walter Roberson
Walter Roberson 2024년 5월 8일
I do not understand how this solves the problem that was asked about.

댓글을 달려면 로그인하십시오.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by