Subs does not calculate result
2 views (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
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)
vpa(val)
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))
double(y1(0.4))
0 Comments
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)
s = sqrt(two)
vpa(s, 10) % Display to 10 decimal places
format longg
d = double(s) % convert to double precision
oldprefvalue = sympref('FloatingPointOutput', true); % Change the preferences
disp(s)
% Reset the preferences
sympref('FloatingPointOutput', oldprefvalue);
disp(s) % Should behave the same as the second line of code above
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!