Tag plus is protected when defining a function according to Fourier series of certain function - wolfram-mathematica

I am defining a function according to Fourier series of certain function, and I got the error 'Tag Plus is protected'.
Could someone help me with it? Many thanks in advance for any suggestion.
Here is the code:
FourierCosSeries[x^2, x, 5]
g[x_]=1-2 (FourierCosSeries[x^2, x, 5])

Related

Curve fit does not return expected result

I need a little help with my code during curve fitting some data.
I have the following data:
'''
x_data=[0.0, 0.006702200711821348, 0.012673613376102217, 0.01805805116486128, 0.02296065262674275, 0.027460615301376282,
0.03161908492177514, 0.03548425629114566, 0.03909479074665314, 0.06168416627459879, 0.06395092768264225,
0.0952415360565632, 0.0964823380829502, 0.11590819258911032, 0.11676250975220677, 0.18973251809768016,
0.1899603458289615, 0.2585011532435637, 0.2586068948029052, 0.40046782450999047, 0.40067753715444315]
y_data=[0.005278154532534359, 0.004670803439961002, 0.004188802888597246, 0.003796976494876385, 0.003472183813732432,
0.0031985782141146, 0.002964943046115825, 0.0027631157936632137, 0.0025870148284089897, 0.001713418196416643,
0.0016440241050665323, 0.0009291243501697267, 0.0009083385934116964, 0.0006374601714823219, 0.0006276132323039056,
0.00016900738921547616, 0.00016834735819595378, 7.829234957755694e-05, 7.828353274888779e-05, 0.00015519569743801753,
0.00015533437619227267]
'''
I know that the data can be fitted using the following mathematical model:
'''
def model(x,a,b,c):
return (ab)/(bx+1)+3cx**2
'''
I am trying to obtain the a,b,c coefficients of the model calibrated, so that I obtain the following result (in red is the model calibrated and in blue is the data sample):
My code to achieve the shown result in the former picture is:enter image description here
'''
import numpy as np
from scipy.optimize import curve_fit
popt, _pcov = curve_fit(model, x_data, y_data,maxfev = 100000)
x_sample=np.linspace(0,0.5,1000)
y_sample=model(x_sample,*popt)
'''
If I plot the predicted data based on the fitted coefficients (in green) I get this result:enter image description here
for some reason I get some coefficients that produce a result I know it is wrong. Does anyone know how to solve this issue?
Your model y=(ab)/(bx+1)+3cx**2 appears not really satisfising. Instead of the hyperbolic term an exponential term seems better according to the shape of the data. That is why the proposed model is :
y=A * exp(B * x) + C * x**2
The method to compute approximates of the parameters A,B,C is shown below :
Details of the numerical calculus :
Note :
The parabolic term appears under represented. This is because they are not enough points at large x compare to the many points at small x.
The method used above is explained in https://fr.scribd.com/doc/14674814/Regressions-et-equations-integrales. The method isn't iterative and doesn't need initial "guessed" values. The accuracy is not good in case of few points, due to the numerical integration (calculus of the Sk).
If necessary, this can be improved thanks to post-treatment with non-linear regression starting from the above approximative values of the parameters;
An even better model is made of two exponentials :

H2O Frame apply function on each row in python

I am looking for a method similar to the 'apply' function in pandas. I tried
my_H2Oframe.apply(lambda x: my_function(x), axis=1)
But this doesn't work.
ValueError: Unimplemented: op < my_function > not bound in H2OFrame
I found this question. It seems we can only use those functions that have already been defined by H2O. I think there must be a method similar to the apply function because this is a common operation. Does anyone have a solution?
There is no other apply type method at the moment. the H2O apply method is suppose to be a close equivalent to pandas apply. It is true that H2O's apply function is limited to certain operations such as addition (+), subtraction (-), division, etc. If you use one that H2O doesn't have you will get the error above.
here are a few examples to try to see how the apply function can work (first one gets the mean across columns, the second returns a boolean column):
h2oframe = h2o.import_file("http://h2o-public-test-data.s3.amazonaws.com/smalldata/prostate/prostate.csv")
h2oframe.apply(lambda x: x.mean(), axis=0)
h2oframe.apply(lambda x: x['PSA'] > x['VOL'],axis=1)
And here is the current documentation on it:
apply(fun=None, axis=0):
Apply a lambda expression to an H2OFrame.
Parameters:
fun – a lambda expression to be applied per row or per column.
axis – 0 = apply to each column; 1 = apply to each row
Returns:
a new H2OFrame with the results of applying fun to the current frame.

Displaying results of solve function in TI-89 programming (cubic equation)

I have a program to find principal stresses. In it, is a cubic equation that I would like to solve and display 3 results. Here are my questions:
1. is solve() function correct one to use in ti-basic to solve the cubic equation? if not, which is correct?
2. When executing the program in home screen how to display three values of cubic equation?
Here is my program:
princstr(xx,yy,zz,xy,xz,yz)
Prgm
a = xx+yy+zz
b = xx*yy+yy*zz+zz*xx-xy^2-yz^2+xz^2
c = xx*yy*zz+xy*yz*xz+xz*xy*yz-xz*yy*xz-xz*yy*xz-yz*yz*xx-zz*xy*xy
solve(s^3-a*s^2+b*s-c=0,s)
Return s
EndPrgm
In the home screen it just returs
Done
Thank you in advance!
P.s. Edits are welcome.
You are not displaying S. Return does not work like return in other languages. In TI-Basic, Return does not return a value; it return control to the calling program. If you want to display it, you should do
:Disp S
:Return

pymc3 how to code multi-state discrete Bayes net CPT?

I'm trying to build a simple Bayesian network, where rain and sprinkler are the parents of wetgrass, but rain and sprinkler each have three (fuzzy-logic type rather rather than the usual two boolean) states, and wetgrass has two states (true/false). I can't find anywhere in the pymc3 docs what syntax to use to describe the CPTs for this -- I'm trying the following based on 2-state examples but it's not generalizing to three states the way I thought it would. Can anyone show the correct way to do this? (And also for the more general case where wetgrass has three states too.)
rain = mc.Categorical('rain', p = np.array([0.5, 0. ,0.5]))
sprinker = mc.Categorical('sprinkler', p=np.array([0.33,0.33,0.34]))
wetgrass = mc.Categorical('wetgrass',
mc.math.switch(rain,
mc.math.switch(sprinker, 10, 1, -4),
mc.math.switch(sprinker, -20, 1, 3),
mc.math.switch(sprinker, -5, 1, -0.5)))
[gives error at wetgrass definition:
Wrong number of inputs for Switch.make_node (got 4((, , , )), expected 3)
]
As I understand it - switch is a theano function similar to (b?a:b) in a C program; which is only doing a two way comparison. It's maybe possible to set up the CPT using a whole load of binary switches like this, but I really want to just give a 3D matrix CPT as the input as in BNT and other bayes net libraries. Is this currently possible ?
You can code a three-way switch using two individual switches:
tt.switch(sprinker == 0,
10
tt.switch(sprinker == 1, 1, -4))
But in general it is probably better to index into a table:
table = tt.constant(np.array([[...], [...]]))
value = table[rain, sprinker]

how to use units along function parameter values in Mathematica

I would like to pass the parameter values in meters or kilometers (both possible) and get the result in meters/second.
I've tried to do this in the following example:
u = 3.986*10^14 Meter^3/Second^2;
v[r_, a_] := Sqrt[u (2/r - 1/a)];
Convert[r, Meter];
Convert[a, Meter];
If I try to use the defined function and conversion:
a = 24503 Kilo Meter;
s = 10198.5 Meter/Second;
r = 6620 Kilo Meter;
Solve[v[r, x] == s, x]
The function returns the following:
{x -> (3310. Kilo Meter^3)/(Meter^2 - 0.000863701 Kilo Meter^2)}
which is not the user-friendly format.
Anyway I would like to define a and r in meters or kilometers and get the result s in meters/second (Meter/Second).
I would be very thankful if anyone of you could correct the given function definition and other statements in order to get the wanted result.
Here's one way of doing it, where you use the fact that Solve returns a list of rules to substitute a value for x into v[r, x], and then use Convert, which will do the necessary simplification of the resulting algebraic expression as well:
With[{rule = First#Solve[v[r,x]==s,x]
(* Solve always returns a list of rules, because algebraic
equations may have multiple solutions. *)},
Convert[v[r,x] /. rule, Meter/Second]]
This will return (10198.5 Meter)/Second as your answer.
You just need to tell Mathematica to simplify the expression assuming that the units are "possitive", which is the reason why it doesn't do the simplifications itself. So, something like
SimplifyWithUnits[blabla_, unit_List]:= Simplify[blalba, (#>0)&/#unit];
So if you get that ugly thing, you then just type %~SimplifyWithUnits~{Meter} or whatever.

Resources