How to compute numeric approximation of symbolic expression? - octave

I have a derivative of a function, and Octave displays it as an expression:
10____ ⎛ 10 ⎞
╲╱ 11 ⋅⎜- ── + log(11)⎟
⎝ 11 ⎠
This is nice and tidy, and completely accurate. However, I would like to get a numerical approximation of said expression, without manually typing the expression (which seems counter-intuitive to me). I can't figure out how to do this, but I probably just don't know what exactly to search for (English is not my native language), and therefore I can't tell if this question is a duplicate.
This is how I got there:
octave> f(x)=(1+(1/x))^x
f(x) = (symfun)
x
⎛ 1⎞
⎜1 + ─⎟
⎝ x⎠
octave> F(x)=diff(f(x))
F(x) = (symfun)
x
⎛ 1⎞ ⎛ ⎛ 1⎞ 1 ⎞
⎜1 + ─⎟ ⋅⎜log⎜1 + ─⎟ - ─────────⎟
⎝ x⎠ ⎜ ⎝ x⎠ ⎛ 1⎞⎟
⎜ x⋅⎜1 + ─⎟⎟
⎝ ⎝ x⎠⎠
octave> F(0.1)
warning: passing floating-point values to sym is dangerous, see "help sym"
[...]
10____ ⎛ 10 ⎞
╲╱ 11 ⋅⎜- ── + log(11)⎟
⎝ 11 ⎠

I think you need the eval function
https://octave.org/doc/v4.0.1/Evaluation.html
For instance
>> eval("log(11)")
ans = 2.3979

Related

let maxima display an exponentiation as a function instead of a caret

maxima accepts both a^b and a**b as input for exponentiation, and will always output the exponent with caret ^.
Is it also possible to get the output as a function, like pow(a,b)?
OK, as you said, you want to output Math.pow(a,b) for Javascript. The approach I'll suggest here is to replace a^b expressions in Maxima with Math.pow(a,b) expressions and output that.
(%i1) e : sqrt(a) + b^(3/2) + 1/c + exp(d^f);
f
d 1 3/2
(%o1) %e + - + b + sqrt(a)
c
(%i2) subst ("^"=lambda([a, b], Math.pow(a, b)), e);
3 1
(%o2) Math . pow(c, - 1) + Math . pow(b, -) + Math . pow(a, -)
2 2
+ Math . pow(%e, Math . pow(d, f))
OK, so that's most of the work there. Some expressions are represented as "^" expressions even if they appear to be something else, for example, sqrt(a) is a^(1/2) and 1/c is c^(-1). If you need for those to be preserved as sqrt(a) and 1/c then we'll have to work on that.
I'm guessing it's best to have floating point values instead of integer ratios. Also, we'll replace %e by its numerical value. If you want %e^x to be rendered as Math.exp(x), we can work on that. Or if you want Math.pow(Math.E, x), that's relatively simple; just evaluate subst(%e = Math.E, <your expression>).
(%i3) float (%);
(%o3) Math . pow(c, - 1.0) + Math . pow(b, 1.5) + Math . pow(a, 0.5)
+ Math . pow(2.718281828459045, Math . pow(d, f))
Maxima considers x . y to mean noncommutative multiplication, but that doesn't come into play here so that's fine. By default it is displayed with a space on either side of the dot, but if you're willing to do a tiny amount of Lisp hacking we can remove the space. (I guess it doesn't matter to Javascript, right? Math . pow is equivalent to Math.pow, isn't it?)
(%i4) :lisp (setf (get 'mnctimes 'dissym) '(#\.))
(.)
(%i4) %o3;
(%o4) Math.pow(c, - 1.0) + Math.pow(b, 1.5) + Math.pow(a, 0.5)
+ Math.pow(2.718281828459045, Math.pow(d, f))
OK, now we can output the expression.
(%i5) grind (%o3);
Math.pow(c,-1.0)+Math.pow(b,1.5)+Math.pow(a,0.5)
+Math.pow(2.718281828459045,Math.pow(d,f))$
(%o5) done
Is that the expected output?
OP asked about converting %e^x to exp(x). That's easy to do, but to make it stick, we have to disable simplification, i.e. the application of identities which Maxima uses to find a general representation of an expression. By default Maxima simplifies exp(x) to %e^x. We can replace %e^x by exp(x) but we need to disable simplification to prevent it from going back again.
(%i1) simp:false $
(%i2) matchdeclare (xx, all) $
(%i3) defrule (to_exp, %e^xx, Math.exp(xx));
xx
(%o3) to_exp : %e -> Math . exp(xx)
(%i4) apply1 (1 + %e^(x + %e^y), to_exp);
(%o4) 1 + Math . exp(x + Math . exp(y))
Probably you only want to disable simplification (i.e. simp:false) when you are ready to output the expression. But I can imagine situations in which you would have it disabled, e.g. if it is important to output the expression exactly the way it was entered, e.g. x + x instead of 2*x.
I've used a different mechanism to do the replacement here, namely defrule which defines a pattern matching rule. Pattern matching is very useful, and I encourage you to take a look at defrule and matchdeclare in the Maxima documentation.

Octave program to solve ODE

I have an ODE
dy=x dx, y(0)=2
The solution of this equation is y =x^2/2 + K.
K become 2.
Now I have to plot graph.
When
x=0, y=2
x=1, y=2.5
x=2, y=4
x=3, y=6.5
x=4, y=10
I have to write an Octave program to generate these values
My code is test.m
function xdot = f (x,t)
xdot=x;
endfunction
x=lsode("f",2,(t=linspace(0,4,5)));
#plot(t,x)
x
I run the pgm in cmd but it gives
2.0000
5.4366
14.7781
40.1711
109.1963
The expected result is
2
2.5
4
6.5
10
Please help me..
Finally got the answer..
Use
xdot=t;
instead of
xdot=x;

Any way to "visualize" a thunk/function? Or how to view a function for a general argument

I'm not totally sure how to ask this, but is there a way to show the structure of a thunk?
For example
f x = x + 2
g x = 3 x
compo x = f (g x)
ans = compo 5
-- result: (3 * 5) + 2 = 17
Is there any way I could "see" the thunk for ans? As in, I could see the process of the beta reduction for compo or like the "general" form.
I would like to see, for example:
compo n
--> (3 * n) + 2
As in, if I had a function compo x, I would like to view that it is decomposed to (3*n)+2.
For example, in Mathematica:
f[x_] := x+2;
g[x_] := 3*x;
compo[x_] := f[g[x]];
compo[n]
(%
--> (3 * n) + 2
%)
There is the ghc-vis package on hackage which show a visualization of your heap and of unevaluated thunks.
See the package on hackage or the Homepage (which contains rather impressive examples).
If you just want to see the sequence of reductions, you could try using the GHCi interactive debugger. (It's in the GHC manual somewhere.) It's not nearly as easy as your typical IDE debugger, but it more or less works...
In general (we are talking about Haskell code) I think it has no sense, the final thunk stream will be different for different input data and, on the other hand, functions are expanded partially (functions are not only simple expressions).
Anyway, you can simulate it (but ugly)
Prelude> :set -XQuasiQuotes
Prelude> :set -XTemplateHaskell
Prelude> import Language.Haskell.TH
Prelude> import Language.Haskell.TH.Quote
Prelude> runQ [| $([|\x -> 3 * x|]) . $([|\y -> y + 2|]) |]
InfixE (Just (LamE [VarP x_0] (InfixE (Just (LitE (IntegerL 3))) (VarE GHC.Num.*) (Just (VarE x_0))))) (VarE GHC.Base..) (Just (LamE [VarP y_1] (InfixE (Just (VarE y_1)) (VarE GHC.Num.+) (Just (LitE (IntegerL 2))))))

Mathematica: finding Extrema positions with Solve, Reduce and FindRoot. (derivative)

I want to determinate the local maxima and minima of the following 2 functions
xE[t_] := 10 (t - Sin[t]) - Sqrt[40^2 - (10 (1 - Cos[t]))^2]
vE = xE'[t]
So I tried to solve the first derivate of xE[t] with:
extremaXE = Solve[vE[t] == 0, t] (* vE is the 1st derivative of xE *)
but I got this error:
Solve::ifun: Inverse functions are being used by Solve, so some solutions may not
be found; use Reduce for complete solution information.
I tried then with reduce and I got this error:
Reduce::nsmet: This system cannot be solved with the methods available to Reduce
so what should I do to determinate the local minima and maxima through the derivatives?
Use NLOpt.
It has algorithms to find local/global extrema with/without derivatives.
It is callable from C, C++, Fortran, Matlab or GNU Octave, Python, GNU Guile, and GNU R.
http://ab-initio.mit.edu/wiki/index.php/NLopt
Does this help?
I don't get an error with Reduce. For example, to find the local extrema of xE I tried
Reduce[xE'[t] == 0, t]
which returned
C[1] \[Element] Integers && (t == 2 \[Pi] C[1] ||
t == 2 I ArcTanh[2/Sqrt[3]] + 2 \[Pi] C[1])
Note that this gives you both real and complex solutions. If you only want the real ones you can try
Reduce[xE'[t] == 0, t, Reals]
which gives
C[1] \[Element] Integers && t == 2 \[Pi] C[1]
Edit
To substitute the solutions back into the original expression you could convert it to a list of rules using for example ToRules. Since ToRules can't handle expressions like C[1] \[Element] Integers we simplify the solution first
sol = Reduce[xE'[t] == 0, t];
sol = Simplify[sol, C[_] \[Element] Integers]
(* ==> t == 2 \[Pi] C[1] || t == 2 I ArcTanh[2/Sqrt[3]] + 2 \[Pi] C[1] *)
ToRules will then convert this expression to a list of rules which you can substitute back into your expression using ReplaceAll
xE[t] /. {ToRules[sol]}
(* ==> {-Sqrt[1600 - 100 (1 - Cos[2 \[Pi] C[1]])^2] +
10 (2 \[Pi] C[1] - Sin[2 \[Pi] C[1]]),
-Sqrt[1600 - 100 (1 - Cosh[2 ArcTanh[2/Sqrt[3]] - 2 I \[Pi] C[1]])^2] +
10 (2 I ArcTanh[2/Sqrt[3]] + 2 \[Pi] C[1] -
I Sinh[2 ArcTanh[2/Sqrt[3]] - 2 I \[Pi] C[1]])} *)
Note that the resulting expression still contains the constant C[1]. To find the extrema for a particular value of C[1] you can use another replacement rule, e.g.
({t, xE[t]} /. {ToRules[sol]}) /. {C[1] -> -4}

Matlab plotting the shifted logistic function

I would like to plot the shifted logistic function as shown from Wolfram Alpha.
In particular, I would like the function to be of the form
y = exp(x - t) / (1 + exp(x - t))
where t > 0. In the link, for example, t is 6. I had originally tried the following:
x = 0:.1:12;
y = exp(x - 6) ./ (1 + exp(x - 6));
plot(x, y);
axis([0 6 0 1])
However, this is not the same as the result from Wolfram Alpha. Here is an export of my plot.
I do not understand what the difference is between what I am trying to do here vs. plotting shifted sin and cosine functions (which works using the same technique).
I am not completely new to Matlab but I do not usually use it in this way.
Edit: My values for x in the code should have been from 0 to 12.
fplot takes as inputs a function handle and a range to plot for:
>> fplot(#(x) exp(x-6) / (1 + exp(x-6)), [0 12])
The beauty of fplot in this case is you don't need to spend time calculating y-values beforehand; you could also extract values from the graph after the fact if you want (by getting the line handle's XData and YData properties).
Your input to Wolfram Alpha is incorrect. It is interpreted as e*(x-6)/(1-e*(x-6)). Use plot y = exp(x - 6) / (1 + exp(x - 6)) for x from 0 to 12 in Wolfram Alpha (see here) for the same results as in MATLAB. Also use axis([0 12 0 1]) (or no axis statement at all on a new plot) to see the full results in MATLAB.
In reply to your comment: use y = exp(1)*(x - 6) ./ (1 + exp(1)*(x - 6)); to do in MATLAB what you were doing in Wolfram Alpha.