0

I fitted some function via Mathematica in a way like this

parameter = {a, b};
parameter
data = {{0, 1.2}, {0, 0.1}, {0.1, 0.2}, {1.1, 0}}
Ftest[x_, y_] := a*x^2 + b*y^2
fit = FindMinimum[Total[(Ftest @@@ data - 2)^2], parameter]
ContourPlot[(Ftest[x, y] /. fit[[2]]) == 2, {x, 0, 1.5}, {y, 0, 1.5}, 
Epilog -> {Red, Point /@ data}]

With the appropriate outcome. However my actual function is more complex and consists of a sinus/cosinus/arccos functions with some exponent to the power of 8. Use my actual code with the actual function I get some error:

The function value {3.74166 (-2.+81. (256. Power[<<2>>]+256. Power[<<2>>]+256. Power[<<2>>]) (Times[<<4>>]+Times[<<2>>])^4)^2} is not a list of real numbers with dimensions {1} at {apb3d,bpb3d,cpb3d,fpb3d,gpb3d,hpb3d} = {1.,1.,1.,1.,1.,1.}. >>

The function I want to fit is in an area for x of 0 and 90 whereas y is between 0 and 2. However since it is a series of sinus functions the fitting function is symertical The parameters I want to fit are between the values of -2 and 2. Some test with known parameter values have been performed so the actual function is correct. Does Mathematica need some further input of where to limit the search for the parameters to?

YaY
  • 333
  • 1
  • 4
  • 14
  • Since the error message says YourFunction isn't Real when {apb3d,bpb3d,cpb3d,fpb3d,gpb3d,hpb3d} = {1.,1.,1.,1.,1.,1.} try YourFunction/.{apb3d->1.,bpb3d->1.,cpb3d->1.,fpb3d->1.,gpb3d->1.,hpb3d->1.} and see what the result is. – Bill Apr 05 '17 at 17:09
  • You can specify constraints on the `FindMinimum` parameters. Look in the docs the are several examples. – agentp Apr 05 '17 at 18:18
  • When I insert the values for apb3d = 1 and the other parameters as well my function looks as it should. However I get some small "waves" in my function which dont appear when I plot the function in another prorgam, e.g. Excel. – YaY Apr 06 '17 at 08:52
  • Since the error message you showed says the result is not {somerealnumber} when you make those substitutions I was trying to get you to find out what the result was that was different from {somerealnumber} and I would expect that would point you at the problem. Without any more information about the function, other than your general statements, it is very difficult to be any more helpful. – Bill Apr 06 '17 at 21:59

2 Answers2

1

Thank you for all the details of what you are trying to do. That is essential to getting an answer that you can use.

To try to be very precise about this to track down the error, If I evaluate this

mpb3d=8; sigxx=x; sigyy=y; sigzz=0; sigxy=z; sigxz=0; sigyz=0;
Ab3d=(sigyy-sigzz)*apb3d; Bb3d=(sigzz-sigxx)*bpb3d;
Cb3d=(sigxx-sigyy)*cpb3d; Fb3d=(sigyz)*fpb3d;
Gb3d=(sigxz)*gpb3d; Hb3d=(sigxy)*hpb3d;
I2b3d=(Fb3d^2+Gb3d^2+Hb3d^2)/3+((Ab3d-Cb3d)^2+(Cb3d-Bb3d)^2+(Bb3d-Ab3d)^2)/54;
I3b3d=((Cb3d-Bb3d)*(Ab3d-Cb3d)*(Bb3d-Ab3d))/54+Fb3d*Gb3d*Hb3d-
  ((Cb3d-Bb3d)*Fb3d^2+(Ab3d-Cb3d)*Gb3d^2+(Bb3d-Ab3d)*Hb3d^2)/6;
thetab3d = ArcCos[I3b3d/((I2b3d)^(3/2))];
phib3d=(3*I2b3d)^(mpb3d/2)*((2*Cos[(2*thetab3d+Pi)/6])^mpb3d +
  (2*Cos[(2*thetab3d-3*Pi)/6])^mpb3d+(-2*Cos[(2*thetab3d+5*Pi)/6])^mpb3d);
nYoFIT3D112212={{-(160/313),160/313,0},{1,0,0},{290/313,21/313, 78/313},
  {236/313,79/313,137/313},{8/17,8/17,152/313},{76/313,227/313,131/313},
  {21/313,294/313,79/313},{0,333/313,0}};
parameter = {apb3d, bpb3d, cpb3d, hpb3d};
Ftest[x_, y_, z_] := phib3d;
Total[(Ftest @@@ nYoFIT3D112212 - 2)^2]

to see exactly what you are going to give to FindMinimum then it displays

8*(-2+81*(((bpb3d*x+cpb3d*(x-y))^2+(-(bpb3d*x)-apb3d*y)^2+(-(cpb3d*(x-y))+apb3d*y)^2)/54+
  (hpb3d^2*z^2)/3)^4*(256*Cos[(-3*Pi+2*ArcCos[(((bpb3d*x+cpb3d*(x-y))*(-(bpb3d*x)-apb3d*y)*
  etc, etc, etc.

Notice that all your x,y,z remain and none were replaced by the triples in your nYoFIT3D112212 like I assume you were expecting had happened.

This was the key step I was trying to get you to discover.

There are probably several different ways to fix this. One is if I replace

Total[(Ftest @@@ nYoFIT3D112212 - 2)^2]

with

Total[Map[(phib3d-2)^2/.{x->#[[1]],y->#[[2]],z->#[[3]]} &,nYoFIT3D112212]]

then I do see all your x,y,z replaced with the coefficients from nYoFIT3D112212.

Then this

fittning=FindMinimum[
  Total[Map[(phib3d-2)^2/.{x->#[[1]],y->#[[2]],z->#[[3]]}&,nYoFIT3D112212]], parameter]

no longer complains about not being a list of {Real}.

Please check all this very carefully to make certain that the correct substitutions have been made for x,y,z in every step and that there are no other errors lurking in what I have done.

Bill
  • 3,664
  • 1
  • 12
  • 9
  • That worked very well, I needed to add and vary my measuring data a little bit but that is no issue at all. However I have encountered another issue about the displaying of my function while using the line: ContourPlot[(Ftest[x, y, z] /. fittning[[2]]) == 2, {x, 0, 1.5}, {y, 0, 1.5}, {z, 0, 1.5}, Epilog -> {Red, Point /@ nYoFIT3D112212}] Results in: "Options expected (instead of {z,0,1.5`}) beyond position 3 in \ ContourPlot[(Ftest[x,y,z]/. \ fittning[[2]])==2,{x,0,1.5`},{y,0,1.5`},{z,0,1.5`},Epilog->{Red,\ Point/@nYoFIT3D112212}]. An option must be a rule or a list of rules." – YaY Apr 10 '17 at 15:54
  • @YaY First, ContourPlot doesn't accept,{x,0,1.5},{y,0,1.5},{z,0,1.5}. Read the help page for ContourPlot and make your use almost exactly like one of their examples. If that doesn't fix the problem then often the Plot functions will change the way things are evaluated. I suggest defining a complete finished function with a new name and then give that function to Plot instead of trying to create some function on the fly inside the argument to Plot. Next, you might try using Show to combine your Plot with Graphics3D[Point /@ nYoFIT3D112212], again trying to avoid evaluation questions inside Plot – Bill Apr 10 '17 at 23:19
0

To be more precise I added the function I would like to fit. Derived from:

mpb3d = 8;

sigxx = x;
sigyy = y;
sigzz = 0;
sigxy = z;
sigxz = 0;
sigyz = 0;

Ab3d = (sigyy - sigzz)*apb3d;
Bb3d = (sigzz - sigxx)*bpb3d;
Cb3d = (sigxx - sigyy)*cpb3d;
Fb3d = (sigyz)*fpb3d;
Gb3d = (sigxz)*gpb3d;
Hb3d = (sigxy)*hpb3d;

I2b3d = (Fb3d^2 + Gb3d^2 + Hb3d^2)/
    3 + ((Ab3d - Cb3d)^2 + (Cb3d - Bb3d)^2 + (Bb3d - Ab3d)^2)/54;
I3b3d = ((Cb3d - Bb3d)*(Ab3d - Cb3d)*(Bb3d - Ab3d))/54 + 
   Fb3d*Gb3d*
    Hb3d - ((Cb3d - Bb3d)*Fb3d^2 + (Ab3d - Cb3d)*
       Gb3d^2 + (Bb3d - Ab3d)*Hb3d^2)/6;

thetab3d = ArcCos[I3b3d/((I2b3d)^(3/2))];

phib3d = (3*I2b3d)^(mpb3d/2)*((2*Cos[(2*thetab3d + Pi)/6])^
     mpb3d + (2*Cos[(2*thetab3d - 3*Pi)/6])^
     mpb3d + (-2*Cos[(2*thetab3d + 5*Pi)/6])^mpb3d)

Which leads to the function I would like to solve as phib3d:

   1 (1/54 ((bpb3d x + cpb3d (x - y))^2 + (-bpb3d x - 
        apb3d y)^2 + (-cpb3d (x - y) + apb3d y)^2) + (hpb3d^2 z^2)/
   3)^4 (256 Cos[
     1/6 (-3 \[Pi] + 
        2 ArcCos[(1/
              54 (bpb3d x + cpb3d (x - y)) (-bpb3d x - 
                apb3d y) (-cpb3d (x - y) + apb3d y) - 
             1/6 hpb3d^2 (-bpb3d x - apb3d y) z^2)/(1/
              54 ((bpb3d x + cpb3d (x - y))^2 + (-bpb3d x - 
                  apb3d y)^2 + (-cpb3d (x - y) + apb3d y)^2) + (
             hpb3d^2 z^2)/3)^(3/2)])]^8 + 
   256 Cos[1/
      6 (\[Pi] + 
        2 ArcCos[(1/
              54 (bpb3d x + cpb3d (x - y)) (-bpb3d x - 
                apb3d y) (-cpb3d (x - y) + apb3d y) - 
             1/6 hpb3d^2 (-bpb3d x - apb3d y) z^2)/(1/
              54 ((bpb3d x + cpb3d (x - y))^2 + (-bpb3d x - 
                  apb3d y)^2 + (-cpb3d (x - y) + apb3d y)^2) + (
             hpb3d^2 z^2)/3)^(3/2)])]^8 + 
   256 Cos[1/
      6 (5 \[Pi] + 
        2 ArcCos[(1/
              54 (bpb3d x + cpb3d (x - y)) (-bpb3d x - 
                apb3d y) (-cpb3d (x - y) + apb3d y) - 
             1/6 hpb3d^2 (-bpb3d x - apb3d y) z^2)/(1/
              54 ((bpb3d x + cpb3d (x - y))^2 + (-bpb3d x - 
                  apb3d y)^2 + (-cpb3d (x - y) + apb3d y)^2) + (
             hpb3d^2 z^2)/3)^(3/2)])]^8)

with the variables x,y,z (stresses in 11,22 and 12 direction) and the parameters apb3d,bpb3d, cpb3d, hpb3d.

And the fitting data:

   nYoFIT3D112212 = {{-(160/313), 160/313, 0}, {1, 0, 0}, {290/313, 21/313, 78/313}, {236/
      313, 79/313, 137/313}, {8/17, 8/17, 152/313}, {76/313, 227/313, 131/
      313}, {21/313, 294/313, 79/313}, {0, 333/313, 0}}

I want to solve via:

parameter = {apb3d, bpb3d, cpb3d, hpb3d};

Ftest[x_, y_, z_] := phib3d

fittning = 
 FindMinimum[Total[(Ftest @@@ nYoFIT3D112212 - 2)^2], parameter]

ContourPlot[(Ftest[x, y, z] /. fittning[[2]]) == 2, {x, 0, 1.5}, {y, 
   0, 1.5}, {z, 0, 1.5}, Epilog -> {Red, Point /@ nYoFIT3D112212}];
YaY
  • 333
  • 1
  • 4
  • 14