f(x) = x^2/3(3-x^2)(x-4). How do I enter this function in MATLAB?

30 ビュー (過去 30 日間)
Preethika
Preethika 2024 年 9 月 14 日
回答済み: John D'Errico 2024 年 9 月 14 日
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);
Warning: Solutions are parameterized by the symbols: z2. To include parameters and conditions in the solution, specify the 'ReturnConditions' value as 'true'.
Warning: Solutions are only valid under certain conditions. To include parameters and conditions in the solution, specify the 'ReturnConditions' value as 'true'.
cmin = min(double(c));
Error using mupadengine/evalin2double
Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables.

Error in mupadengine/feval2double

Error in sym/double (line 745)
X = feval2double(symengine, "symobj::doubleDirect", S);
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 件のコメント
Torsten
Torsten 2024 年 9 月 14 日
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
Star Strider 2024 年 9 月 14 日
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
x_zeros = 1×3
0 1.7288 3.9986
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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
x_zeros = 1×2
0.7387 3.1204
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
hold on
plot(xq2, zeros(size(xq2)), 'rs')
hold off
hold(Ax1)
Current plot held
for k = 1:numel(xq2)
yq1(k) = interp1(xv1, yv1, xq2(k));
plot(Ax1, xq2(k), yq1(k), 'gs')
end
hold off
.

サインインしてコメントする。

回答 (2 件)

Torsten
Torsten 2024 年 9 月 14 日
Replace
c = solve(fx);
by
c = solve(fx*x^(1/3)==0,'Maxdegree',3);

John D'Errico
John D'Errico 2024 年 9 月 14 日
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)
c = struct with fields:
x: z2^3 parameters: z2 conditions: 11*z2^9 + 24 == 32*z2^6 + 15*z2^3 & in(z2^3, 'real') & -pi/3 < angle(z2) & angle(z2) <= pi/3
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)
ffun = function_handle with value:
@(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.

Community Treasure Hunt

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

Start Hunting!

Translated by