Message Boards Message Boards

1
|
4437 Views
|
7 Replies
|
5 Total Likes
View groups...
Share
Share this post:

How do I make a nonlinear equation based on data I generate?

Posted 10 years ago
Hi,

I am new to this forum and Mathematica and could really use your help [and patience].

My ultimate goal is to get a simple 4th degree polynomial equation that I can code into a program.  Here is the method I'm using to acheive this goal:

I have an equation
y = x + x^2 + x^3 + x^4
that I need to solve for multiple positive values of y.  For example, I used
Solve[x + x^2 + x^3 + x^4 == .55]
which gives me x=0.358668 [I'm only interested in the positive value of x].  For my analysis, y can range between 0 and 100.  I started to manually change it's value .05 at a time, plugging it into Solve[] and writing down each of the calculated values.  This is obviously very manually intensive.  

Once I got enough values to see if it would work [48 in this case], I used regression analysis in Excel to create an equation
k = -0.0075j^4 + 0.0706j^3 - 0.2737j^2 + 0.6621^j + 0.0672
that I can code in my program.  I need a more efficient way to do this and thought that I could do it all in Mathematica...

Something like:
  1. for y=0 to 100, step .05, solve for x where y = x + x^2 + x^3 + x^4 [this would produce a dataset of y and x values where the y values go from 0, 0.05, 0.10, 0.15, ... 99.95, 100.0 and a corresponding x value is generated from solving the equation]
  2. using the output dataset from above and regression analysis, create a 4th degree polynomial equation like k above.
  3. is this doable in Mathematica?
Any help/suggestions would be greatly appreciated.

Thanks,

Neil
POSTED BY: Neil Greisman
7 Replies
Posted 10 years ago
This may be what you are looking for, it solves all 10,000 equations, stores them in a list and uses Fit on the data.  Seperate the ans part so you can try different modles once the data has been calculated.


Clear[x]; list = {}; list = Reap[Do[a = Solve[x + x^2 + x^3 + x^4 == n && x > 0, Reals];
a = x /. a; Sow[{n, a}], {n, 0.1, 100, .01}]]; list =
Partition[Flatten[list[[2]]], 2]; ans = TraditionalForm[Fit[list, {1, x, x^2, x^3, x^4}, x]]

-9.78157*10^-8 x^4+0.0000237103 x^3-0.00209162 x^2+0.0917249 x+0.606326

Paul.
POSTED BY: Paul Cleary
Posted 10 years ago
Thanks Paul!  I'll give it a try and see how it works.
POSTED BY: Neil Greisman
Posted 10 years ago
Szabolcs, you are correct, the approximation is not good [I thought I was being clever but not-so-much].  I do not know how to do what you are suggesting.  Could you please share what you've found?

Thanks,

Neil
POSTED BY: Neil Greisman
Neil, if you are looking to use the inverse of this function (x+x^2+x^3+x^4) in your program, it might be better to implement a simple numerical solver (Newton's method) tailored to this specific equation rather than approximating it with another fourth order polynomial (the approximation is not going to be good---I just checked).  I found this way to be very efficient for evaluating the roots of polynomials in numerical codes.  It is also simple to implement.
POSTED BY: Szabolcs Horvát
Posted 10 years ago
Very cool... I'm quite sure it would have taken me a very long time [if ever]  to get to what you've provided .  Thank you very much Bill.
POSTED BY: Neil Greisman
Posted 10 years ago
See if you can use this
Map[Flatten, Table[Select[{x,y}/.Solve[x+x^2+x^3+x^4==y,x], Element[#[[1]],Reals]&&#[[1]]>=0&], {y,0,100,.05}]]
Then take that bit by bit, and usually from the "inside out" by understanding how each part of each argument to each function works first. Look the bits up in the help system and try to figure out what the strategy was to do this. Why was Table used? Why was there a Select inside that? Can you identify the second argument given to Select and how does that puzzling notation work? You can also try running simplified versions of this, discovering what happens when you don't include some parts. what do you get if you don't use the Map[Flatten,...], but just use the ... part? All this should help you greatly in the future.
POSTED BY: Bill Simpson
Hello and welcome to the Wolfram Community! Please take a few minutes to read this tutorial about correct posting – especially of Mathematica code:

How to type up a post: editor tutorial & general tips

If you will not follow the tutorial, other members of community may not be able to test your code. To edit your post – click “Edit” in the lower right corner of your post.
POSTED BY: Moderation Team
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