https://wiki.octave.org/wiki/api.php?action=feedcontributions&user=71.80.253.48&feedformat=atom Octave - User contributions [en] 2020-03-29T13:18:41Z User contributions MediaWiki 1.34.0 https://wiki.octave.org/wiki/index.php?title=Symbolic_package&diff=10320 Symbolic package 2017-04-22T20:07:43Z <p>71.80.253.48: /* Demos and usage examples */</p> <hr /> <div>The {{Forge|symbolic|symbolic package}} is part of the octave-forge project.<br /> <br /> [[Category:Octave-Forge]]<br /> <br /> === Demos and usage examples ===<br /> <br /> * '''I'm trying to substitute a double value into an expression. How can I avoid getting &quot;warning: Using rat() heuristics for double-precision input (is this what you wanted?)&quot;.'''<br /> <br /> In general, you should be very careful when converting floating point (&quot;doubles&quot;) to symbolic variables, that's why the warning is bothering you.<br /> <br /> &lt;source lang=&quot;octave&quot;&gt;<br /> <br /> ## Demo of how to use a number (which was calculated in an octave<br /> ## variable) in a symbolic calculation, without getting a warning.<br /> <br /> ## use octave to calculate some number:<br /> a = pi/2<br /> <br /> ## now do some work with the symbolic pkg<br /> syms x<br /> f = x * cos (x)<br /> df = diff (f)<br /> <br /> ## Now we want to evaluate df at a:<br /> <br /> # subs (df, x, a) # this gives the &quot;rats&quot; warning (and gives a symbolic answer)<br /> <br /> ## So instead, try<br /> <br /> dfh = function_handle (df)<br /> <br /> dfh (a)<br /> <br /> ans = -1.5708<br /> <br /> <br /> ## And you can evaluate dfh at an array of &quot;double&quot; values:<br /> <br /> dfh ([1.23 12.3 pi/2])<br /> <br /> ans =<br /> -0.82502 4.20248 -1.57080<br /> <br /> &lt;/source&gt;<br /> <br /> &lt;source lang=&quot;octave&quot;&gt;<br /> <br /> ##Demo of how to graph symbolic functions (by converting SYMBOLIC functions into ANONYMOUS functions)<br /> ##The following code will produce the same vector field plot as Figure 1.14 from Example 1.6 (pg. 39) from A Student's Guide to Maxwell's Equations by Dr. Daniel Fleisch.<br /> <br /> ##Make sure symbolic package is loaded and symbolic variables declared.<br /> pkg load symbolic;<br /> syms x y<br /> <br /> ##Write a Vector Field Equation in terms of symbolic variables <br /> vector = [sin(pi .* y ./2 ); -sin(pi .* x ./2 )];<br /> <br /> ##Vector components are converted from symoblic into anonymous form which allows them to be graphed<br /> ##The '1' &amp; '2', in vector(1) &amp; vector(2) are indeces for the vector.<br /> ##The &quot; 'vars', [x y]&quot; syntax tells function_handle that each component is a function of both 'x' &amp; 'y'<br /> iComponent = function_handle (vector(1), 'vars', [x y]); <br /> jComponent = function_handle (vector(2), 'vars', [x y]); <br /> <br /> ##Setting the Grid up to graph all axes for all variables from -.5 to .5, with spacings .05 apart. <br /> [X,Y] = meshgrid([-.5:.05:.5]);<br /> <br /> ##Draw the Vector Field!<br /> figure<br /> quiver(X,Y,iComponent(X,Y),jComponent(X,Y))<br /> <br /> &lt;/source&gt;<br /> <br /> <br /> * '''Demo of Anonymous function to symbolic function and back to anonymous function and then the use of the interval pkg.'''<br /> <br /> &lt;source lang=&quot;octave&quot;&gt;<br /> % this is just a formula to start with,<br /> % have fun and change it if you want to.<br /> <br /> f = @(x) x.^2 + 3*x - 1 + 5*x.*sin(x);<br /> <br /> % these next lines take the Anonymous function into a symbolic formula<br /> <br /> pkg load symbolic<br /> syms x;<br /> ff = f(x);<br /> <br /> % now calculate the derivative of the function<br /> <br /> ffd = diff(ff, x);<br /> <br /> % and convert it back to an Anonymous function<br /> <br /> df = function_handle(ffd)<br /> <br /> <br /> % this uses the interval pkg to find all the roots between -15 an 10 <br /> <br /> pkg load interval<br /> fzero (f, infsup (-15, 10), df)<br /> <br /> ans ⊂ 4×1 interval vector<br /> <br /> [-5.743488743719015, -5.743488743719013]<br /> [-3.0962279604822407, -3.09622796048224]<br /> [-0.777688831121563, -0.7776888311215626]<br /> [0.22911205809043574, 0.2291120580904359]<br /> &lt;/source&gt;<br /> <br /> <br /> * '''Demo of inputting a function at the input prompt and making an Anonymous function.'''<br /> <br /> &lt;source lang=&quot;octave&quot;&gt;<br /> # This prog. shows how to take a<br /> # string input and make it into an anonymous function<br /> # this uses the symbolic pkg.<br /> disp(&quot;Example input&quot;)<br /> disp(&quot;x^2 + 3*x - 1 + 5*x*sin(x)&quot;)<br /> str_fucn=input(&quot;please enter your function &quot;,&quot;s&quot;)<br /> fucn_sym=sym(str_fucn)<br /> f=function_handle(fucn_sym)<br /> # now back to symbolic<br /> syms x;<br /> ff=formula(f(x));<br /> % now calculate the derivative of the function <br /> ffd=diff(ff);<br /> % and convert it back to an Anonymous function<br /> df=function_handle(ffd)<br /> % now lets do the second derivative<br /> ffdd=diff(ffd);<br /> ddf=function_handle(ffdd)<br /> % and now plot them all <br /> x1=-2:.0001:2;<br /> plot(x1,f(x1),x1,df(x1),x1,ddf(x1))<br /> grid minor on<br /> legend(&quot;f&quot;,&quot;f '&quot;, &quot;f '' &quot;)<br /> &lt;/source&gt;<br /> <br /> <br /> * '''Demo of ODE with a step input and initial conditions.'''<br /> <br /> &lt;source lang=&quot;octave&quot;&gt;<br /> <br /> ## This is a demo of a second order transfer function and a unit step input.<br /> ## in laplace we would have 1 1<br /> ## _______________ * _____<br /> ## s^2 + sqrt(2)*s +1 s<br /> ##<br /> ## So the denominator is s^3 + sqrt(2) * s^2 + s<br /> # and for laplace initial conditions area<br /> ## t(0)=0 t'(0) =0 and the step has initial condition of 1<br /> ## so we set t''(0)=1<br /> ## In the code we use diff(y,1)(0) == 0 to do t'(0)=0<br /> ##<br /> ## I know that all this can be done using the control pkg<br /> ## But I used this to verify that this solution is the<br /> ## same as if I used the control pkg.<br /> ## With this damping ratio we should have a 4.321% overshoot.<br /> ##<br /> syms y(x) <br /> de =diff(y, 3 ) +sqrt(2)*diff(y,2) + diff(y) == 0;<br /> f = dsolve(de, y(0) == 0, diff(y,1)(0) == 0 , diff(y,2)(0) == 1)<br /> ff=function_handle(rhs(f))<br /> x1=0:.01:10;<br /> y=ff(x1);<br /> plot(x1,y)<br /> grid minor on <br /> <br /> &lt;/source&gt;</div> 71.80.253.48