Subs does not calculate result
Afficher commentaires plus anciens
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?
Réponses (2)
Dyuman Joshi
le 25 Oct 2023
Modifié(e) : Dyuman Joshi
le 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))
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
Catégories
En savoir plus sur Conversion Between Symbolic and Numeric dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
