Write from maple to a text file - maple

I have the following code in maple:
K:= log(x);
for j from 2 by 1 to 10 do evalf(subs(x=j,K)) end do;
I want to write each x and log(x) value to a TEXT File in 2 columns.
Any help please ?

John M's answer is more general in that it should work in any version of Maple from Maple 6 onwards, but if you happen to be using Maple 2015, 2016, or 2017, then you could use the Export command which autodetects the format from the file extension:
M := Matrix(9,2):
for j to 9 do M[j,1] := j+1; M[j,2] := evalf(log(j+1)); end do:
Export("C:\\Users\\yourname\\Documents\\MyFile.csv", M);
or if you want a one-liner:
Export("C:\\Users\\yourname\\Documents\\MyFile.csv",Matrix(9,2,(i,j)->`if`(j=1,i+1,evalf(log(i+1))))):

The easiest option is to assign these values to a nx2 Matrix and then ExportMatrix it to text file:
M := LinearAlgebra:-RandomMatrix(10, 2);
ExportMatrix("C:\\Users\\yourname\\Documents\\FileName.txt",
M, target = MATLAB, mode = ascii);

Related

How to use Maple to solve equation normalization coefficient

I want to ask for the normalization coefficient of the solution f(x).
The implementation method in Mathematica is as follows:
f[x] := Sin[(n Pi x)/(2 a)];
norm = FullSimplify[1/Integrate[Abs[f[x]]^2, {x, 0, a}], n \[Element] Integers]
The final results are as follows:
2/a
I want to implement the same function in Maple, and have tried the following:
assume(n::posint);
psi := x -> sin(1/2*n*pi*x/a);
c := 1/int(abs(psi(x))^2, x = 0 .. a);
simplify(solve(c = 1, a));
Is there a good way to deal with it if it is implemented with Maple?
restart;
kernelopts(version);
Maple 2022.0, X86 64 LINUX, Mar 8 2022, Build ID 1599809
assume(n::posint);
f := abs(sin(1/2*n*Pi*x/a))^2:
simplify(combine(1/int(convert(f,Heaviside,x),
x=0..a)));
2
-
a
simplify(combine(1/int(f,x=0..a)))
assuming a::real;
2
-
a

hyperpolic function integration in maple

Im trying to evaluate this function in maple
but I keep getting this answer, why isn't maple integrating properly. I tried numerically integrating it and it works but I need the analytical solution too.
restart;
sig := x->(exp((x-t)/a)-exp((-x-t)/a))
/(exp((x-t)/a)+exp((-x-t)/a)):
new := convert(simplify(convert(expand(sig(x)),trigh)),tanh);
new := tanh(x/a)
simplify(expand(convert(sig(x) - new, exp)));
0
Now, you originally wrote int(f*sig(x)/x,x).
You didn't indicate that f was a function of x, and as a mere constant it's not really important and could simply be pulled out in front of the integral as a constant factor. If f is some function of x then you really need to state what it is!
Let's consider int(sig(x)/x,x=c..d). Using the simplification new, that is just,
Q := Int( new/x, x=c..d );
Q := Int(tanh(x/a)/x, x = c .. d)
QQ := IntegrationTools:-Change(Q, y=x/a, y);
QQ := Int(tanh(y)/y, y = c/a .. d/a)
You said that you wanted an "analytical solution" by which I take it you mean an explicit formula for the symbolic integration result. But what do you want if the integral does not (mathematically) have a closed form exact, symbolic result?
Would you be content with an (exact, symbolic) series approximation?
H := (a,ord,c,d)
-> int(convert(series(eval(new/x,:-a=a),x,ord),
polynom),x=c..d):
# order 5
H(a, 5, c, d);
3 3 / 5 5\
d - c -c + d 2 \-c + d /
----- - -------- + ------------
a 3 5
9 a 75 a
For a specific example, taking a=2 and an (exact) series approximation of order 25, then the integral from x=0 to x=1 gets evaluated as an exact rational.
evalf(H(2, 25, 0, 1));
0.4868885956
Here's the numeric integration for those same values,
evalf(Int( eval(new/x,a=2), x=0..1 ));
0.4868885956
Specialized numeric quadrature could be as good as a series approximation for a variety of applications, but of course that would depend on what you intend on doing with the result.
This raises the question: what do you hope to do with some supposed "analytical result" that you cannot do with a black-box function that generates the floating-point numeric approximation? Why do you "need" an "analytic result"?
BTW, another way to simplify it (in case the construction above of new does not succeed in your Maple version):
new := convert(simplify(expand( numer(sig(x))/exp(-t/a) ))
/simplify(expand( denom(sig(x))/exp(-t/a) )),
compose,trigh,tanh);
/x\
new := tanh|-|
\a/

how to solve a matrix equation in maple

I have a program that does some work to get a matrix w, which is 3(n+1) by 3(n+1). I have a vector fbar that is 3(n+1) by 1. I want to get the matrix that, when w is multiplied by it, gives fbar.
In mathematical notation, w * A = fbar. I have w and fbar, and I want A.
I tried to solve it with this command:
fsolve({seq(multiply(w, A)[i, 1] = fbar[i, 1], i = 1 .. 3*(n+1))})
but I don't understand the response Maple gave:
fsolve({2.025881905 A1[2,1]+7.814009150 A1[3,1]+...
-7.071067816 10^(-13) A1[3,1]-0.0004999999990
A1[4,1]-0.0007071067294 A1[5,1]-0.0004999999990 A1[6,1])
A3[6,1]=0},{A1[1,1],A1[2,1],A1[3,1],A1[4,1],A1[5,1],A1[6,1],A\
2[1,1],A2[2,1],A2[3,1],A2[4,1],A2[5,1],A2[6,1],A3[1,1],A3[2,1]\
,A3[3,1],A3[4,1],A3[5,1],A3[6,1]})
What does this mean, and how can I get a more meaningful answer?
You can do this directly with the LinearSolve functional from the LinearAlgebra package if w and fbar are defined as a matrix and vector respectively. The below code makes a reproducible example. Note that the solution of LinearSolve should be equal to x.
w := Matrix(<<1,2,3>|<4,5,6>|<7,8,10>>);
LinearAlgebra[ReducedRowEchelonForm](%); ## Full rank => 1 solution)
x := <1,2,3>;
fbar := w.x;
## Solve the equation w.x = fbar
LinearAlgebra[LinearSolve](w,fbar);

rewrite MATLAB to Maple

I have a small MATLAB symbolic script as following
syms z;
psi(2)=exp(2*z-exp(z))/(1-exp(-exp(z)));
psi(3)=exp(2*z-exp(z))/(1-exp(-exp(z)))*z;
psi(4)=exp(2*z-exp(z))/(1-exp(-exp(z)))*z^2;
f(1,1)=exp(2*z-exp(z))/(1-exp(-exp(z)));
for i=2:4
f(i,1)=diff(psi(i),z);
for j=2:i
f(i,j)=diff(f(i,j-1)/f(j-1,j-1),z);
end
end
given a symbolic vector psi consist of functions of z, it create a lower triangle symbolic matrix f. it works well.
I'm trying to rewrite this part in Maple, which I'm new to. I tried
psi(2) := exp(2*z-exp(z))/(1-exp(-exp(z)));
psi(3) := exp(2*z-exp(z))*z/(1-exp(-exp(z)));
psi(4) := exp(2*z-exp(z))*z^2/(1-exp(-exp(z)));
f(1, 1) := exp(2*z-exp(z))/(1-exp(-exp(z)));
for i from 2 to 4 do f(i,1):=exp(2*z-exp(z))/(1-exp(-exp(z)));
for j from 2 to i do f(i,j):=diff(f(i,j-1)/f(j-1,j-1),z);
od;
od;
something ambiguous in the "diff" line, I just select function definition. if I let it output f(4,4), it report
Error, (in f) too many levels of recursion
but it did print f(4,1).
could some one tell what's wrong? Thanks!
Your code is pretty close (and reminds me how similar these two languages are at times). The reason for the error message is that you need to declare f before you start filling it with values.
Here's one possible solution:
psi[2] := exp(2*z-exp(z))/(1-exp(-exp(z)));
psi[3] := exp(2*z-exp(z))*z/(1-exp(-exp(z)));
psi[4] := exp(2*z-exp(z))*z^2/(1-exp(-exp(z)));
f := Matrix(1..4,1..4):
f[1, 1] := exp(2*z-exp(z))/(1-exp(-exp(z))):
for i from 2 to 4 do
f[i,1] := diff(psi[i],z):
for j from 2 to i do
f[i,j] := diff(f[i,j-1]/f[j-1,j-1],z):
end do:
end do:
f;
Note here that I declare f to be a 4x4 Matrix before I start filling it. Also, here the [] notation is used for specifying indices.
Another option which may scale better for larger problems is to grow your data structure for f as you add values to it. Here we start with a 1x1 Array and add values to it.
psi[2] := exp(2*z-exp(z))/(1-exp(-exp(z)));
psi[3] := exp(2*z-exp(z))*z/(1-exp(-exp(z)));
psi[4] := exp(2*z-exp(z))*z^2/(1-exp(-exp(z)));
f:=Array(1..1,1..1):
f(1, 1) := exp(2*z-exp(z))/(1-exp(-exp(z))):
for i from 2 to 4 do
f(i,1):=diff(psi[i],z):
for j from 2 to i do
f(i,j):=diff(f[i,j-1]/f[j-1,j-1],z):
end do:
end do:
f;
Here you'll notice that we are using the () notation for Array indices at time of creation. If you use an Array for storage, this is one technique that allows for you to grow the Array as you add values.
Now in both cases you can also note that I've used [] to index a term that already exists; square brackets are the default notation in Maple for specifying indices in a data structure.
Also note that I've suppressed output in each loop using the : operator; this way you can just echo back the resulting Matrix f at the end.

Custom correlation function in matlab

I am trying to create a custom function for pearson's correlation coefficient with this code in matlab 2010
function [p] = customcorr(o)
x := a
y := b
x_mean := mean(a)
y_mean := mean(b)
x_std := std(a)
y_std := std(b)
n := length(o)
r := (1/(n-1))*((x-x_mean)*(y-y_mean))/(x_std*y_std)
end
But i get an error when trying to execute it
Error in ==> customcorr at 2
x := a
Anybody might know what the problem is? Thank you
First, check the correct MATLAB syntax: a "normal" assignment is done by =, not by :=.
Second, you use a and b but these are not defined as parameters of the function. Replace the function head by function p = customcorr(a,b).
Third, I am not really sure what o should be, I assume it can be replaced by length(a) or length(b).
The estimator for an unbiased correlation coefficient is given by
(from wikipedia)
Thus you need to sum all the (a-a_mean).*(b-b_mean) up with sum. Note that it is required to write .* to get the element-wise multiplication. That way you subtract the mean from each element of the vectors, then multiply the corresponding a's and b's and sum up the results of these multiplications.
Together this is
function p = customcorr(a,b)
a_mean = mean(a);
b_mean = mean(b);
a_std = std(a);
b_std = std(b);
n = length(a);
p = (1/(n-1)) * sum((a-a_mean).*(b-b_mean)) / (a_std*b_std);
end
What MATLAB does in their corr function (besides many other interesting things) is, they check the number of arguments (nargin variable) to see if a and b were supplied or not. You can do that by adding the following code to the function (at the beginning)
if nargin < 2
b = a;
end