Wednesday, July 1, 2020

Demo of inputting a function at the input prompt

The script get function at the prompt and calculate the first and the second order derivative
of the function. The script can be run on Octave platform.
Download the file demo_prompt_function.m

%     Demo of inputting a function at the input prompt
%     and making an Anonymous function.

% This program. shows how to take a
% string input and make it into an anonymous function
% this uses the symbolic pkg.
% Load Symbolic pkg (Octave only)
pkg load symbolic
setenv PYTHON C:\ProgramData\Anaconda3\pythonw.exe

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 at interva (-2) - 2
x1=-2:.001:2;
plot(x1,f(x1),x1,df(x1),x1,ddf(x1))
grid minor on
legend("f","f '", "f '' ")

The result for input function cos(x)

Example input
x^2 + 3*x - 1 + 5*x*sin(x)
please enter your function  cos(x)
str_fucn = cos(x)
fucn_sym = (sym) cos(x)
f =

@(x) cos (x)

df =

@(x) -sin (x)

ddf =

@(x) -cos (x)
The Graph





No comments:

Post a Comment

Featured Post

Solve simple Absolute Value Inequality

  Exam - BARTON COLLEGE PRACTICE PLACEMENT TEST Ex. 39 Exercise: The inequality |8 - x| < 8   is equivalent to:   a) x < 0 b) x &g...