You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.2 KiB

  1. /*Quadratic Curve Fitting Solution
  2. Find a plausable quadratic fit to a sample of points
  3. Nigel_Galloway@operamail.com
  4. February 1st., 2009
  5. */
  6. set Sample;
  7. param Sx {z in Sample};
  8. param Sy {z in Sample};
  9. var a;
  10. var b;
  11. var c;
  12. equalz1 :sum{z in Sample} a*Sx[z]*Sx[z]*Sx[z]*Sx[z] + sum{z in Sample} b*Sx[z]*Sx[z]*Sx[z] + sum{z in Sample} c*Sx[z]*Sx[z] = sum{z in Sample} Sy[z]*Sx[z]*Sx[z];
  13. equalz2 :sum{z in Sample} a*Sx[z]*Sx[z]*Sx[z] + sum{z in Sample} b*Sx[z]*Sx[z] + sum{z in Sample} c*Sx[z] = sum{z in Sample} Sy[z]*Sx[z];
  14. equalz3 :sum{z in Sample} a*Sx[z]*Sx[z] + sum{z in Sample} b*Sx[z] + sum{z in Sample} c = sum{z in Sample} Sy[z];
  15. solve;
  16. printf "\nbest quadratic fit is:\n\ty = %f %s %fx %s %fx^2\n\n", c, if b < 0 then "-" else "+", abs(b), if a < 0 then "-" else "+", abs(a);
  17. data;
  18. param:
  19. Sample: Sx Sy :=
  20. 1 0 1
  21. 2 0.5 0.9
  22. 3 1 0.7
  23. 4 1.5 1.5
  24. 5 1.9 2
  25. 6 2.5 2.4
  26. 7 3 3.2
  27. 8 3.5 2
  28. 9 4 2.7
  29. 10 4.5 3.5
  30. 11 5 1
  31. 12 5.5 4
  32. 13 6 3.6
  33. 14 6.6 2.7
  34. 15 7 5.7
  35. 16 7.6 4.6
  36. 17 8.5 6
  37. 18 9 6.8
  38. 19 10 7.3
  39. ;
  40. end;