Message Boards Message Boards

1
|
10452 Views
|
2 Replies
|
3 Total Likes
View groups...
Share
Share this post:

How to combine continual and discrete function?

Posted 12 years ago
I'm building one strategy war game, and I want to create mathematics function for hits chance (y axis) based on distance (x axis). I defined manualy one discrete function with coordinate pairs that will serve as main function, then I need to add another (continual) function that will serve as an increase of hits chance.

Discrete Function:
cList = {{0, 100}, {1, 95}, {5, 80}, {10, 70}, {15, 40}, {20,
   25}, {25, 15}, {30, 10}}
ListLinePlot[cList]
Continual Function:
Plot[Sin[2 x^2]*x/4 + Sin[0.3 x] + (8/30)*x, {x, 0, 30}]

I need combined function that I can use later in C++ programming language to calculate hits chance.
for example: if is target and shooting unit at distance 5, then I will use randomly choosen point on combined function from interval X  [4.5, 5.5]

So, first question is: "How to combine those functions?" and second is "How to use that combined function in my C++ application?"
POSTED BY: Borko Djurovic
2 Replies
Posted 12 years ago
Well, I found solution. I was blinded with graphical reprezentation of functions. I forgot for a moment that I have same functions in Math library in C++ emoticon But Wolfram helped a lot because I can see function and can do improvement by adding some changes in function definition, but I'm realy interested about my fisrst question.

Thanks.
POSTED BY: Borko Djurovic
The most conceptually simple answer must be to use programming instead of mathematics to add these two functions.
f[x_]:= Which[x<1, 100, x<2, 95, x<5, 80, ...]
g[x_]:=Sin[2 x^2]*x/4 + Sin[0.3 x] + (8/30)*x
h[x_]:=f+g

In terms of C-implementation, and efficiency, there could be some nonobvious solutions.  For instance, convert the discrete function to a continuous function by making it piecewise linear, and then maybe using abs to make it a single expression.  Then they can all be added together. 

But to be more practical, you probably want to approximate.  A linear fit works well enough possibly but you can try other functions too, e.g.,
Fit[{{0, 100}, {1, 95}, {5, 80}, {10, 70}, {15, 40}, {20, 25}, {25,
   15}, {30, 10}}, {1, x, Sin[(Pi/15) x]}, x]
POSTED BY: Todd Rowland
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract