-3

I'm new to Go, so this is probably simple but hours of tutorials have still left me unsure. I'm running a physics simulator that deals with magnetic nanoparticles. In this simulator (vinamax), you can define the magnetic field as a function of time, t, a variable that is defined in the program. But rather than use simple math functions like sin(b*t), I want to use actual data from an experiment to define the function. I have a table with time values and corresponding magnetic field values at that time. I want to call my "function" instead of a predetermined math function as a lookup table. Would I need to define it as part of the package the simulator is running in? If so how would I be able to tell the calling method to pick the magnetic field value that most closely matches the current time index of my function (assuming the program's time indices aren't exactly matched to my "function's" defined time points)? Thanks for any advice.

egen
  • 1
  • You don't need to define it in the same package, and probably shouldn't, since it's really independent functionality. But of course you need to make that function public (uppercase first letter) in the package where it is defined (and import the package!). As for matching the time values, that's a problem you need to work out - there's no generic answer. Clearly you need an intermediary function to convert between the differing t values. – see sharper Sep 01 '20 at 02:42
  • 1
    Instead of `sin(b*t)` call `interpolateMeasurementData(t)` and look up the two measurements before and after t and interpolate. How you interpolate depends on the actual experiment and data. – Volker Sep 01 '20 at 03:57

1 Answers1

0

I don’t know that times in your data is equally divided?

Maybe you can new a map, time transfer to unix timestamp as key, and magnetic field values as the value.

In your function ,match keys use passed time argument by binary search algorithm.

ulyC
  • 330
  • 3
  • 11
  • Thank you ulyC and @sharper. The data is equally divided. I have tried making an array with the data (time in one and magnetic field values in another) but it seems that making an array over about 1250 elements results in an error message that the curly braces are "out of range". Is this normal? That seems like a strange feature of a language that arrays are limited like this. Are maps different than this somehow? – egen Sep 02 '20 at 09:34