I would like to compute coefficient in mathematica. for example I wrote this code to find the coefficients of cos(kx) in (a+b*cos(x))^4 where "a" and "b" are parameters.
f[x_] := (a + b Cos[x])^4
f1[x_] := TrigReduce[f[x]]
g[x_, k_] := Coefficient[f1[x], Cos[k x]]
it works for the coefficients of cos(k*x),
for example the coefficient of cos(2x) is
g[x,2]= 1/8 (24 a^2 b^2 + 4 b^4)
but it does not work for constant(here constant means independent of cos(kx). In other words, just the terms with numbers and the parameters "a" and "b").
I would like to write the code to find the constant in the above meaning.
thanks.
Plugging Coefficient[TrigReduce[(a + b*Cos[x])^4],Cos[2*x]] into Wolfram|Alpha produced the output you wanted it to. This leads me to suggest that your problem might have to do with how the expression is being evaluated as opposed to a problem with how you are mathematically thinking about it.
I do not have access to a copy of Mathematica so can not test this, but I would try changing := to = in the second line of code.
I would also try putting it all in one line as
g[x_, k_] := Coefficient[TrigReduce[(a + b Cos[x])^4], Cos[k x]]
If that works, it is definitely something wrong with how/when Mathematica is assigning stuff.
See this link for more information.
Related
I'm trying to solve a simple linear system in Maxima using solve like so:
/*Standard form*/
eq1 : x1 + 3*x2 + s1 = 6;
eq2 : 3*x1 + 2*x2 + s2 = 6;
base1 : solve([eq1,eq2],[s1,s2]);
This however returns an empty list and I don't know why. Any ideas? I'm pretty sure the system has a solution, so that shouldn't be the issue.
EDIT:
I attempted to insert the equations explicitly into solve in place of eq1 and eq2, and now it works. Now the question is, why do I need to explicitly insert the equations to be solved for into the first argument of solve. An in-depth answer about how Maxima works in this case would be welcome.
This happened to me when one of the variables in the equation was previously defined.
E.g., if z was previously defined:
Then simply changing z to, e.g., p returns the solutions:
I was wondering how to make Mathematica simplify (x^2(y^2+1))^(5/2)*(y^2+1)^(3/2) to x^5(y^2+1)^4?
Simplify
FullSimplify
You can use FullSimplify[<expression>] to cause Mathematica to attempt to simplify your expression using a variety of strategies. You might need to include some of your assumptions about x and y to get the result you expect. For example, are x and y both real numbers? Then you could try:
FullSimplify[(x^2(y^2+1))^(5/2)*(y^2+1)^(3/2), {Element[{x,y},Reals]}]
In:
(x^2 (y^2 + 1))^(5/2)*(y^2 + 1)^(3/2) // Assuming[Element[{x,y},Reals], Simplify[#]] &
Out:
(1 + y^2)^4 Abs[x]^5
I derived a solution for a driven damped oscillator using the DSolve functionality of Wolfram Mathematica. I chose the following Ansatz:
DSolve[{
0 == -Fmax (1 - Cos[t]) + k x[t] +
d Derivative[1][x][t] +
m Derivative[2][x][t],
x[0] == xStart, x'[0] == vStart}, x[t], t]
Mathematica is able to solve this analytically and I can derive solutions if I pass parameters to the variables.
Anyhow, the derived solution fails at the moment, the given parameters for mass (m), spring constant (k) and dampening (d) fulfill the condition for aperiodic behavior
d=2*Sqrt[m*k]
due to the fact, that a division by zero occurs. The overall solution is given by a big fraction with a denominator containing the following factor
Sqrt[d^2 - 4 k m]
This factor becomes zero in case aperiodic conditions occur which leads to a devision by zero.
Now the fun starts ...
I case I pass the aperiodic conditions before giving the system to DSolve:
DSolve[{
0 == -Fmax (1 - Cos[t]) + k x[t] +
2 Sqrt[k m] Derivative[1][x][t] +
m Derivative[2][x][t],
x[0] == xStart, x'[0] == vStart}, x[t], t]
DSolve easily derives a working solution without dividing by zero.
It seems to me, as Mathematica is simplifying the more general solution, in a way that I can't use the aperiodic conditions anymore, even though there is a working solution for that case. My question is, can I somehow tell DSolve, to take into account the aperiodic conditions?
Sure, I can workaround the problem with Piecewise[], but for further calculations a general solution without Piecewise[] would be more suitable.
Thanks in advance for you time and help,
Greetings
Tschibi
maple code, no matter write this matrix in procedure or not, still get error, how to summation to infinity
DetAn:= (n)-> LinearAlgebra:-Determinant(
Matrix(
n, n,
(i,j)->
if j >= i and (j-i)::even then
(j-i+1)*(j-1)!/(i-1)!*a(j-i+1)*x
elif i-j = 1 then -1
else 0
end if
)
):
Summation(DetAn(k)*z^k/k!, k=0..infinity);
Update:
a(i) could be a := t -> t^2
You will get an error for the given input because the sum (or Summation) command has normal evaluation rules for procedure arguments and so will try to evaluate DetAn(n) for nonumeric symbolic n. You'd get the same error message (from the Matrix constructor) if you just called,
DetAn(n);
where n is an unassigned name.
But delaying that premature evaluation isn't going to get a result.
Summation('DetAn'(k)*z^k/k!, k=0..infinity);
LinearAlgebra:-Determinant is not going to cough up a closed form result for symbolic n. You can get a recursive summation formula for DetAn(n), ie. as a sum of terms involving DetAn(j-1) or DetAn(j-2) from j=1..n/2. I don't know whether you could hammer on that for a generating function.
Consider what kind of answer you are looking for, if only from the Determinant call. Are hoping for a nested sum (nested to a fixed, finite depth)?
What is a(i)?
Why is the determinant in terms of powers of x, while z comes into the summation terms?
Mathematica can simply take infintiy as a limit:
Sum[(1/2)^i, {i, 0, Infinity}]
Out= 2
I didn't try with your example but its worth a shot.
I was wondering if it is possible to use NMinimize from mathematica with an objective function, which contains random variables? E.g. I have a function with parameters which follow a distribution (normal and truncated normal). I want to fit its histogram to data that I have and constructed an objective function which now I need to minimize (so, the objective function depends on the mus and sigmas of the parameters and need to be determined). If I run my code, there is an error message: It claims the parameter for the NormalDistribution needs to be positive (If I plug in numbers for the mus and sigmas of my objective functionby hand, i don't get an error message).
So, I am wondering if it is not possible for NMinimize to handle a non-analytic function.
Thanks!
Here, I give you an example code (please note that the original function is more complicated)
listS and listT are both lists of event times. I want to fit the curve of my statistical model for the times (here, a very simple one, it consists of a truncated normal distribution) to the data I have.
For this I compare the survival curves and need to minimize the sum of the least squares.
My problem is that the function NMinimize doesn't seem to work. (Please note, that the original objective function consists of a more complicated function with parameters that are random variables)
(* Both lists are supposed to be the list of times *)
SurvivalS[listeS_, x_] := Module[{res, survivald},
survivald = SurvivalDistribution[listeS];
res = SurvivalFunction[survivald, x];
res]
Residuum[listeT_, listeS_] :=
Table[(SurvivalS[listeT, listeT[[i]]] - SurvivalS[listeS, listeT[[i]]]), {i,
1, dataN}];
LeastSquare[listeT_, listeS_] :=
Total[Function[x, x^2] /#
Residuum[listeT,
listeS]];(* objective function, here ist is the sum of least square *)
objectiveF[mu_, sigma_] :=
Piecewise[{{LeastSquare[listeT, listeS[mu, sigma]], mu > 0 && sigma > 0}},
20 (1 + (sigma + mu)^2)];
pool = 100; (* No. points from MonteCarlo *)
listeS[mu_, sigma_] := RandomVariate[TruncatedDistribution[{0, 1}, NormalDistribution[mu, sigma]],pool];(* simulated data *)
listeT = Sort[RandomVariate[TruncatedDistribution[{0, 1}, NormalDistribution[.5, .9]],60]]; (* list of "measured" data *)
dataN = Length[listeT];
NMinimize[objectiveF[mu, .9], {{mu, .4}}]
The error message is: "RandomVariate::realprm: Parameter mu at position 1 in NormalDistribution[mu,0.9] is expected to be real. >>"