f(x) = x^2/3(3-x^2)(x-4). How do I enter this function in MATLAB?
29 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
When I input the above function as x^(2/3)*(3-x^2)*(x-4) in a code where i have to find extrema , maxima and critical points for a given function , i am getting error which says
Warning: Solutions are parameterized by the symbols: z2. To include parameters and conditions in the solution, specify the 'ReturnConditions'
value as 'true'.
Error using mupadengine/feval2char
Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables.
Error in sym/double (line 755)
Xstr = feval2char(symengine, "symobj::double", S);
however the same cose is working if I input some other function
this is my code
syms x real
% f= input('Enter the function f(x):'); % Edited by Sam: Actual code
f = x^(2/3)*(3-x^2)*(x-4) % Edited by Sam: For running the code in forum
fx= diff(f,x);
c = solve(fx);
cmin = min(double(c));
cmax = max(double(c));
ezplot(f,[cmin-2,cmax+2])
hold on
sprintf('The critical points are %d ',c)
fxx= diff(fx,x);
for i = 1:1:size(c)
T1 = subs(fxx, x ,c(i) );
T3= subs(f, x, c(i));
if (double(T1)==0)
sprintf('The point x is %d inflection point',double (c(i)))
else
if (double(T1) < 0)
sprintf('The maximum point x is %d', double(c(i)))
sprintf('The value of the function is %d', double (T3))
else
sprintf('The minimum point x is %d', double(c(i)))
sprintf('The value of the function is %d', double(T3))
end
end
plot(double(c(i)), double(T3), 'r*', 'markersize', 15);
end
pause
h=ezplot(fx,[cmin-2,cmax+2]);
set(h,'color','r')
hold on
pause
e=ezplot(fxx,[cmin-4,cmax+4]);
set(e,'color','g')
hold off
3 commentaires
Torsten
le 14 Sep 2024
The error does not arise from entering the function, but from what you do with the function. So we cannot tell what the problem is unless we see your code.
Star Strider
le 14 Sep 2024
A numeric approach —
syms x real
% f= input('Enter the function f(x):'); % Edited by Sam: Actual code
f = x^(2/3)*(3-x^2)*(x-4) % Edited by Sam: For running the code in forum
fx= diff(f,x)
figure
hfp1 = fplot(f, [-1 1]*5);
grid
axis('padded')
Ax1 = gca;
xv1 = hfp1.XData;
yv1 = hfp1.YData;
L = numel(xv1);
zxi = find(diff(sign(yv1)));
for k = 1:numel(zxi)
idxrng = max(1, zxi(k)-1) : min(L,zxi(k)+1);
xq1(k) = interp1(yv1(idxrng), xv1(idxrng), 0);
end
x_zeros = xq1
hold on
plot(xq1, zeros(size(xq1)), 'rs')
hold off
figure
hfp2 = fplot(fx, [-1 1]*5);
grid
axis('padded')
xv2 = hfp2.XData;
yv2 = hfp2.YData;
L = numel(xv2);
zxi = find(diff(sign(yv2)));
for k = 1:numel(zxi)
idxrng = max(1, zxi(k)-1) : min(L,zxi(k)+1);
xq2(k) = interp1(yv2(idxrng), xv2(idxrng), 0);
end
x_zeros = xq2
hold on
plot(xq2, zeros(size(xq2)), 'rs')
hold off
hold(Ax1)
for k = 1:numel(xq2)
yq1(k) = interp1(xv1, yv1, xq2(k));
plot(Ax1, xq2(k), yq1(k), 'gs')
end
hold off
.
Réponses (2)
Torsten
le 14 Sep 2024
Replace
c = solve(fx);
by
c = solve(fx*x^(1/3)==0,'Maxdegree',3);
0 commentaires
John D'Errico
le 14 Sep 2024
How do you enter it? You already did exactly that, at least in symbolic form.
syms x real
% f= input('Enter the function f(x):'); % Edited by Sam: Actual code
f = x^(2/3)*(3-x^2)*(x-4) % Edited by Sam: For running the code in forum
fx = diff(f,x);
Differentiating allows you to find the critical points of various types.
c = solve(fx == 0,x,'returnconditions',true)
The problem is, what it found is not terribly useful in terms of a solution. Effectively, in order to find a symbolic solution, it needs to solve for the roots of a degree 9 polynomial. And sadly, this is mathematically impossible to do in an algebraic form. That means you CANNOT use solve. This means you need to use a root finder.
So plot the function, choosing the region to plot it wisely. Note that when x is less than zero, you will be raising a negative number to a fractional power.
fplot(f,[-5,5])
And as you see, that is a problem. However, x^(2/3) could also be written as (x^2)^(1/3) = nthroot(x^2,3), so the real cube root of the square of x.
ffun = @(x) nthroot(x.^2,3).*(3-x.^2).*(x-4)
fplot(ffun,[-3,5])
yline(0)
Is that something your teacher expects you to recognise? Or is the domain of the function expected to be purely positive x? Sigh. Only da shadow knows. Well, and your teacher. My guess is you are expected to look only at the positive real line.
And that means you will need to find those three roots of the first derviative. Just visually here, they will be around 1, 2, and 3. You could use vpasolve, applied to fx, with those start points. Then check to see if the found point is a min, max ,or an inflection point.
HOWEVER...
if (double(T1)==0)
Do NOT check for an exact zero, as it will not be exactly zero. Remember you will be working in terms of floating point arithmetic, even if it is from vpasolve.
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!