The script convert symbolic expression df(x) to funtion dfh(x) using the
function_handle Octave function. The script can run on Octave platform.
Download the file demo_sym.m
Download the file demo_sym.m
% Demo of how to use a number (which was calculated in an octave
% variable) in a symbolic calculation, without getting a warning.
% use octave to calculate some number:
a = pi/2
% now do some work with the symbolic pkg
syms x
f = x * cos (x)
df = diff (f)
% Now we want to evaluate df at a:
% subs (df, x, a) # this gives the "rats" warning (and gives a symbolic answer)
% So instead, try
dfh = function_handle (df)
disp('dfh(a) = ')
dfh (a)
% ans = -1.5708
% And you can evaluate dfh at an array of "double" values:
disp ('dfh ([1.23 12.3 pi/2]) = ')
dfh ([1.23 12.3 pi/2])
% ans =
% -0.82502 4.20248 -1.57080
The Result
a = 1.5708
f = (sym) x*cos(x)
df = (sym) -x*sin(x) + cos(x)
dfh =
@(x) -x .* sin (x) + cos (x)
dfh(a) =
ans = -1.5708
dfh ([1.23 12.3 pi/2]) =
ans =
-0.82502 4.20248 -1.57080
f = (sym) x*cos(x)
df = (sym) -x*sin(x) + cos(x)
dfh =
@(x) -x .* sin (x) + cos (x)
dfh(a) =
ans = -1.5708
dfh ([1.23 12.3 pi/2]) =
ans =
-0.82502 4.20248 -1.57080
No comments:
Post a Comment