31 views (last 30 days)
Show older comments
Liam Wiltshire on 5 Jan 2018
-
-
Link
Direct link to this question
https://matlabcentral.mathworks.com/matlabcentral/answers/375723-using-fzero-with-syms-or-finding-an-alternative-to-syms
Commented: Walter Roberson on 8 May 2024 at 1:26
Accepted Answer: Star Strider
Open in MATLAB Online
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
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Star Strider on 5 Jan 2018
Open in MATLAB Online
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 Comments Show 4 older commentsHide 4 older comments
Show 4 older commentsHide 4 older comments
Liam Wiltshire on 5 Jan 2018
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/375723-using-fzero-with-syms-or-finding-an-alternative-to-syms#comment_521852
⋮
Open in MATLAB Online
Based on an answer you proivided to another question and using this my code now reads:
function Question_1_try2
x = linspace(-10, 10, 40);
y = x.^3 - 3.*x.^2 + 5*x.*sin(pi.*(x./4) - 5*(pi/4)) + 3;
plot(x,y)
grid on
grid minor
title('Graph of y= x^3 - 3*x^2 + 5*x*sin(pi*(x/4) - 5*(pi/4)) + 3 Through -10 < x < 10')
xlabel('-10 < x < 10')
ylabel('y values')
x0 = 0;
rootx = fzero(y, x0);
end
I just can't for the life of me get fzero to process properly, could you help me out to save me having to spam questions on the forum?
Thank you
Star Strider on 5 Jan 2018
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/375723-using-fzero-with-syms-or-finding-an-alternative-to-syms#comment_521862
My pleasure.
First, you need to use the anonymous function I wrote earlier for ‘y’ with fzero, not the fixed variable assignment in your code.
Second, you need to loop through the values of ‘x’, using each ‘x(k)’ value as ‘x0’, where ‘k’ is the loop index. Store each of the results as ‘rootx(k)’. Use a for loop. (Use whatever name you want for the indexing variable.)
Third, your function has 3 real roots, so consider using the uniquetol function to isolate only three (and possibly get some extra points on the assignment).
Since this appears to be homework, I will let you do the coding, limiting my comments to hints.
Liam Wiltshire on 6 Jan 2018
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/375723-using-fzero-with-syms-or-finding-an-alternative-to-syms#comment_521989
Open in MATLAB Online
My code now reads as follows and meets all of the requirements of the questions. Yes it's a little adhoc but it works. Thank you for your help once again.
function Question_1_try3
y = @(x) x.^3 - 3.*x.^2 + 5*x.*sin(pi.*(x./4) - 5*(pi/4)) + 3;
for x = (-10:0.5:10);
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1])
fplot(y, [-10 10])
grid on
grid minor
title('Graph of y= x^3 - 3*x^2 + 5*x*sin(pi*(x/4) - 5*(pi/4)) + 3 Through -10 < x < 10')
xlabel('-10 < x < 10')
ylabel('y values')
end
x0 = [-1];
rootx1 = fzero(y, x0)
x00 = [1];
rootx2 = fzero(y, x00)
x000 = [4];
rootx3 = fzero(y, x000)
hold on
plot(rootx1,y(rootx1), 'rd')
hold on
plot(rootx2,y(rootx2), 'bd')
hold on
plot(rootx3,y(rootx3), 'kd')
legend('Function', 'Root 1', 'Root 2', 'Root 3')
hold off
end
I know it deviates from your guidance at parts, however I've tried to implement what you've said but have apparently left out the finesse of loops and concise code, i am going to come back to it and improve it when I've finished my other coursework.
Again, a huge thank you for your input i couldn't have done it without your help :D
Star Strider on 6 Jan 2018
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/375723-using-fzero-with-syms-or-finding-an-alternative-to-syms#comment_521992
As always, my pleasure!
A loop would be more efficient. You have the essential approach correct.
Miguel D. Rothe on 27 Sep 2020
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/375723-using-fzero-with-syms-or-finding-an-alternative-to-syms#comment_1027024
Open in MATLAB Online
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 on 27 Sep 2020
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/375723-using-fzero-with-syms-or-finding-an-alternative-to-syms#comment_1027036
you can vpasolve() instead of fzero()
Sign in to comment.
More Answers (1)
Enkhtsetseg on 7 May 2024 at 11:20
Open in MATLAB Online
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 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Walter Roberson on 8 May 2024 at 1:26
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/375723-using-fzero-with-syms-or-finding-an-alternative-to-syms#comment_3155042
I do not understand how this solves the problem that was asked about.
Sign in to comment.
Sign in to answer this question.
See Also
Categories
Mathematics and OptimizationSymbolic Math ToolboxMuPADMuPAD Language FundamentalsSpecial Values
Find more on Special Values in Help Center and File Exchange
Tags
- symbolic
- syms
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office