Subs does not calculate result

2 views (last 30 days)
Emirhan Temel
Emirhan Temel on 25 Oct 2023
Edited: Dyuman Joshi on 25 Oct 2023
syms x;
y=x*log(x)-1;
dy=diff(y);
u=y/dy;
du=diff(u);
y1=x-(u/du);
When i run subs(y1,x,0.4), answer is ((2*log(2/5))/5 - 1)/(((5*((2*log(2/5))/5 - 1))/(2*(log(2/5) + 1)^2) - 1)*(log(2/5) + 1)) + 2/5 which is not a useful answer for me. However, when i replace x*log(x)-1 with x^2+2*x+1, it calculates the function for x=0.4 and it shows a numerical result unlike previous attempt.
What could I do?

Answers (2)

Dyuman Joshi
Dyuman Joshi on 25 Oct 2023
Edited: Dyuman Joshi on 25 Oct 2023
You can use vpa() to get the answer as a variable precision symbolic number
syms x
y = x*log(x)-1;
dy = diff(y);
u = y/dy;
du = diff(u);
y1 = x-(u/du);
val = subs(y1, x, 0.4)
val = 
vpa(val)
ans = 
0.43341516854082805627269878141879
To directly get the output by plugging in the input value instead of using subs(), define y as a function of x -
Defining y as a function of x will result in subsequent variables being defined via the operations as a functions of x as well.
syms x
y(x) = x*log(x)-1;
dy = diff(y);
u = y/dy;
du = diff(u);
y1 = x-(u/du);
vpa(y1(0.4))
ans = 
0.43341516854082805627269878141879
To convert the value into a numeric data type, use double (the default numeric data type in MATLAB)
double(y1(0.4))
ans = 0.4334

Steven Lord
Steven Lord on 25 Oct 2023
If you want to see or obtain the floating point approximation to the symbolic answer, use the double or vpa functions or change the symbolic preferences using sympref.
two = sym(2)
two = 
2
s = sqrt(two)
s = 
vpa(s, 10) % Display to 10 decimal places
ans = 
1.414213562
format longg
d = double(s) % convert to double precision
d =
1.4142135623731
oldprefvalue = sympref('FloatingPointOutput', true); % Change the preferences
disp(s)
1.4142
% Reset the preferences
sympref('FloatingPointOutput', oldprefvalue);
disp(s) % Should behave the same as the second line of code above

Tags

Community Treasure Hunt

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

Start Hunting!