Im working on a huge matlab code in which I have a symbolic function G as well as its inverse invG and first derivative g defined using "syms" and I use "matlabFunction" in certain functions. These symbolic functions are called multiple times within various functions in the code. The problem is that my code is toooo slow and I suspect the symbolic functions are to blame.
Im also using "symsum" for some convergent series and sums.
Related
I'm writing some MATLAB code that gives a symbolic equation. The equation has a number of fractional terms where the denominators are different functions. I would like to group the terms with the same denominator. To give an example of what I'm trying to achieve assume the following equation:
[1]
Where the x_i's are different functions in my case. Is there a function in MATLAB that can achieve this? or if you could write an algorithm that would be extremely helpful.
[1]: https://i.stack.imgur.com/TtYGc.png
If you are using Matlab's Symbolic Math Toolbox™ (meaning using syms to create symbolic variables and combining those into functions etc...) then the symplify function should do the trick. For more read: Preforming symbolic computations
I am studying Stochastic calculus, and occasionally we need to compute an integral (from -infinity to +infinity) for some complex distribution. In this case, it was
with the answer on the right. This is the code I put into Matlab (and I have the symbolic math toolbox), which Matlab simply cannot process:
>> syms x t
>> f = exp(1+2*x)*(1/((2*pi*t)^0.5))*exp(-(x^2)/(2*t))
>> int(f,-inf,inf)
ans =
-((2^(1/2)*pi^(1/2)*exp(2*t + 1)*limit(erf((2^(1/2)*((x*1i)/t - 2i))/(2*(-1/t)^(1/2))), x, -Inf)*1i)/(2*(-1/t)^(1/2)) - (2^(1/2)*pi^(1/2)*exp(2*t + 1)*limit(erf((2^(1/2)*((x*1i)/t - 2i))/(2*(-1/t)^(1/2))), x, Inf)*1i)/(2*(-1/t)^(1/2)))/(2*pi*t)^(1/2)
This answer at the end looks like nonsense, while Wolfram (via their free tool), gives me the answer that the picture above has. Am I missing something fundamental about doing such integrations in Matlab that the basic Mathworks pages don't cover? Where am I proceeding incorrectly?
In order to explain what is happening, we need some theory:
Symbolic systems such as Matlab or Mathematica calculate integrals symbolically by the Risch algorithm (yes, there is a method to mechanically calculate integrals, just like derivatives).
However, the Risch algorithms works differently than using derivation rules. Strictly spoken, it is not an algorithm but a semi-algorithm. This is, it is not deterministic one (as algorithms are).
This (semi) algorithm makes a series of transformations on the input expression (the one to be integrated), and in a specific point, it requires to ask if the transformed expression is equals to zero, because if it were zero, it cannot continue (the input is not integrable using a finite set of terms).
The problem (and the reason of the "semi-algoritmicity") is that, the (apparently simple) equation:
E = 0
Is indecidable (it is also called the constant problem). It means that there cannot exist a formal method to solve the constant problem, for any expression E. Of course, we know to solve the constant problem for specific forms of the expression E (i.e. polynomials), but it is impossible to solve the problem for the general case.
It also means that the Risch algorithm cannot be perfect (being able to solve any integral -integrable in finite terms-). In other words, the Risch algorithm will be as powerful as our ability to solve the constant problem for as many forms of the expression E as we can, but losing any hope of solving for the general case.
Different symbolic systems have similar, but different methods to try to solve equations (and therefore the constant problem), it explains why some of them can "solve" different sets of integrals than others.
And generalizing, because no symbolic system will never be able to solve the constant problem for the general case, it will neither be able to solve any integral (integrable in finite terms).
The second parameter of int() needs to be the variable you're integrating over (which looks like t in this case):
syms x t
f = exp(1+2*x)*(1/((2*pi*t)^0.5))*exp(-(x^2)/(2*t))
int(f,'t',-inf,inf) % <- integrate over t
I am new in learning matlab. when I am using the solve() function, matlab warning me that i must use a symbolic variable before using the solve function. but I actually don't know what the sym variable is. or What is the difference between the symbolic variables and the ordinary variables of the base workspace?
Symbolic variables are useful to express equations and manipulate them in an analytic manner. You can use them to manipulte them algebrically, without the need of actually associating any type of numbers.
Suppose you want the exact analytical form of a solution for an equation, in terms of symbols that express this function. Then you can use a sym variable to express and operate with the unkwons, rather than to use on numerical methods to find solutions
Symbolic variables are also useful to operate with tranfer funcions and perform simplications that are very tedious If done without a computer program.
You also can associate numbers with sym variables, once you have done all the all the intended operations.
If you don't want perform operations with algebraic variables, you should checkout the fsolve function
I want to minimize some function J using gradient information. I found two functions in scipy that may do the job, scipy.optimize.fmin_tnc (here) and scipy.optimize.minimize (here), and I implemented them. However, now I need the stepwise output of the function evaluations at each step of the (e.g. Newton) algorithm to plot its convergence. Is it possible to get this vector somehow out of these functions? It is not part of default return values as it seems.
Most of my Matlab functions can be converted with the mfile2sci function to Scilab functions except some functions that contain anonymous functions (for example f=#(x,y)sin(x)+log(y)). Is there a way to convert the anonymous functions to Scilab inline functions (for example, for the previous example deff('[z]=f(x,y)','z=sin(x)+log(y)')) so that I don't have to change my Matlab functions?
Yes, in its current state the Matlab to Scilab translator mfile2sci fails to translate anonymous functions, but this behavior can be improved by the following patch:
https://codereview.scilab.org/#/c/20916/
However, be warned that anonymous function occur most of the time when using "solvers" like fsolve, optim, ode solvers (e.g. ode45, ode15s,...), and that statements using these are never translated to working Scilab statements (a warning is given).