Symbolic package: Difference between revisions

Jump to navigation Jump to search
1,405 bytes added ,  10 June 2019
m
Remove redundant Category:Packages.
m (Remove redundant Category:Packages.)
 
(12 intermediate revisions by 6 users not shown)
Line 1: Line 1:
The {{Forge|symbolic|symbolic package}} is part of the octave-forge project.
The {{Forge|symbolic|symbolic package}} is part of the [[Octave Forge]] project.


[[Category:Octave-Forge]]
[[Category:Octave Forge]]


=== Demos and usage examples ===
=== Demos and usage examples ===
* '''I'm trying to substitute a double value into an expression.  How can I avoid getting "warning: Using rat() heuristics for double-precision input (is this what you wanted?)".'''
In general, you should be very careful when converting floating point ("doubles") to symbolic variables, that's why the warning is bothering you.
<source lang="octave">
## 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)
dfh (a)
ans = -1.5708
## And you can evaluate dfh at an array of "double" values:
dfh ([1.23 12.3 pi/2])
ans =
  -0.82502  4.20248  -1.57080
</source>
* '''Demo of how to graph symbolic functions (by converting SYMBOLIC functions into ANONYMOUS functions)'''
<source lang="octave">
## 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.
## Make sure symbolic package is loaded and symbolic variables declared.
pkg load symbolic
syms x y
## Write a Vector Field Equation in terms of symbolic variables
vectorfield = [sin(pi*y/2); -sin(pi*x/2)];
## Vector components are converted from symbolic into "anonymous functions" which allows them to be graphed.
## The "'vars', [x y]" syntax ensures each component is a function of both 'x' & 'y'
iComponent = function_handle (vectorfield(1), 'vars', [x y]);
jComponent = function_handle (vectorfield(2), 'vars', [x y]);
## Setup a 2D grid
[X,Y] = meshgrid ([-0.5:0.05:0.5]);
figure
quiver (X, Y, iComponent (X, Y), jComponent (X,Y))
</source>


* '''Demo of Anonymous function to symbolic function and back to anonymous function and then the use of the interval pkg.'''
* '''Demo of Anonymous function to symbolic function and back to anonymous function and then the use of the interval pkg.'''
Line 11: Line 79:
% have fun and change it if you want to.
% have fun and change it if you want to.


f=@(x) x.^2 +3*x-1 + 5*x.*sin(x);
f = @(x) x.^2 + 3*x - 1 + 5*x.*sin(x);


% the next 2 line take the Anonymous function into a symbolic formula
% these next lines take the Anonymous function into a symbolic formula


pkg load symbolic
syms x;
syms x;
 
ff = f(x);
ff=formula(f(x));


% now calculate the derivative of the function
% now calculate the derivative of the function


ffd=diff(ff);
ffd = diff(ff, x);


% and convert it back to an Anonymous function
% and convert it back to an Anonymous function


df=function_handle(ffd)
df = function_handle(ffd)




% this uses the interval pkg. to find all the roots between -15 an 10  
% this uses the interval pkg to find all the roots between -15 an 10  


pkg load interval
fzero (f, infsup (-15, 10), df)
fzero (f, infsup (-15, 10), df)


Line 91: Line 160:
  ##
  ##
  syms y(x)  
  syms y(x)  
  de =diff(y, 3 ) +sqrt(2)*diff(y,2) + diff(y) == 0;
sqrt2=sym(1.41421);
  de =diff(y, 3 ) +sqrt2*diff(y,2) + diff(y) == 0;
  f = dsolve(de, y(0) == 0, diff(y,1)(0) == 0 , diff(y,2)(0) == 1)
  f = dsolve(de, y(0) == 0, diff(y,1)(0) == 0 , diff(y,2)(0) == 1)
  ff=function_handle(rhs(f))
  ff=function_handle(rhs(f))
Line 99: Line 169:
  grid minor on  
  grid minor on  


* '''Demo of how to suppress a warning about converting from floating point numbers to symbolic numbers.
## 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)
## Next get the number into a symbolic variable 
## convert to string first 
    aa=mat2str(a)
## and now to a symbolic variable.   
    ww=sym(aa)
## and now use it 
    vpa(subs(df, x, ww), 28)
</source>
</source>

Navigation menu