Symbolic package: Difference between revisions

Line 38: Line 38:
     [-0.777688831121563, -0.7776888311215626]
     [-0.777688831121563, -0.7776888311215626]
     [0.22911205809043574, 0.2291120580904359]
     [0.22911205809043574, 0.2291120580904359]
</source>
* '''Demo of inputting a function at the input prompt and making an Anonymous function.'''
<source lang="octave">
# This prog. shows how to take a
# sring input and make it into an anonymous function
# this uses the symbolic pkg.
disp("Example input")
disp("x^2 + 3*x - 1 + 5*x*sin(x)")
str_fucn=input("please enter your function  ","s")
fucn_sym=sym(str_fucn)
f=function_handle(fucn_sym)
# now back to symbolic
syms x;
ff=formula(f(x));
% now calculate the derivative of the function
ffd=diff(ff);
% and convert it back to an Anonymous function
df=function_handle(ffd)
% now lets do the second derivative
ffdd=diff(ffd);
ddf=function_handle(ffdd)
% and now plot them all
x1=-2:.0001:2;
plot(x1,f(x1),x1,df(x1),x1,ddf(x1))
grid minor on
legend("f","f '", "f '' ")
</source>
</source>


17

edits