1
$\begingroup$

I am trying to fit a function to my data that looks like the image below. This image is a slanted edge that is kept at an angle $a$ w.rt. the y-axis. For this, I am using the error function (Erf[]) in Mathematica.

enter image description here

To model the slant edge I have used the following equation: $$c - c*Erf[xx - yy*Tan[a] - b]$$

where, $c$ changes the amplitude, $a$ is the slant angle w.r.t $yy$-axis, $b$ shifts the overall function.

img = {2.58266, 2.54179, 2.54179, 2.54179, 2.50437, 2.36968, 2.04073, 1.28888, 0.394567, 0.132172, 0.0422067, 0.0287896, 0.0287896, 0.0382885, 0.0382885, 2.67134, 2.6392, 2.58551, 2.58551, 2.52184, 2.37541, 2.05333, 1.21907, 0.377567, 0.135592, 0.0956313, 0.0445234, 0.0445234, 0.0613071, 0.0613071, 2.66889, 2.6549, 2.59497, 2.52573, 2.43016, 2.34504, 2.01896, 1.19275, 0.377, 0.143168, 0.0876763, 0.0429883, 0.0429883, 0.0465403, 0.0552447, 2.66034, 2.6457, 2.58349, 2.50173, 2.43473, 2.31207, 2.01929, 1.17434, 0.343857, 0.106102, 0.0362952, 0.0362952, 0.0220176, 0.0220176, 0.0220176, 2.68638, 2.5651, 2.45718, 2.45718, 2.42628, 2.3394, 2.00395, 1.1587, 0.335028, 0.108957, 0.0336391, 0.0336391, 0.0305434, 0.0304813, 0.0167873, 2.56768, 2.53988, 2.50477, 2.46439, 2.45205, 2.37753, 1.99389, 1.11899, 0.313875, 0.0816565, 0.0471647, 0.0387277, 0.0272155, 0.0281262, 0.0272155, 2.48431, 2.48431, 2.50718, 2.50718, 2.43941, 2.33021, 2.01308, 1.14408, 0.300249, 0.0862527, 0.0428155, 0.0271403, 0.0271403, 0.0271403, -0.000878735, 2.52685, 2.52685, 2.50976, 2.50976, 2.477, 2.34258, 1.99176, 1.11349, 0.345074, 0.0783061, 0.0186313, 0.0143782, 0.0142541, 0.00936227, 0.0142541, 2.60932, 2.56192, 2.52061, 2.52061, 2.52061, 2.38493, 1.93711, 1.10212, 0.32724, 0.11274, 0.0502604, 0.0387917, -0.0055297, -0.0055297, 0.0198825, 2.71946, 2.61243, 2.61243, 2.61137, 2.61137, 2.33598, 1.94251, 1.06084, 0.305351, 0.122695, 0.0162653, 0., 0., 0.0184773, 0.035529, 2.65258, 2.60431, 2.56539, 2.56539, 2.56539, 2.37231, 1.97585, 1.04082, 0.275724, 0.100254, 0.0222655, 0.00877584, 0.00877584, 0.0225156, 0.0319582, 2.59568, 2.59101, 2.54207, 2.54207, 2.5073, 2.41399, 1.94135, 0.993193, 0.246756, 0.0648979, 0.0227677, 0.0227677, 0.0227677, 0.0106321, 0.0106321, 2.58013, 2.58013, 2.56867, 2.58367, 2.56867, 2.39878, 1.9395, 1.00005, 0.251395, 0.0933488, 0.0682032, 0.0266762, 0.0197385, 0.0107465, 0.0107465, 2.58941, 2.58941, 2.62868, 2.62868, 2.61524, 2.39946, 1.88588, 0.943305, 0.258868, 0.113037, 0.0739071, 0.0622146, 0.0211907, 0.0211907, 0.0360544, 2.54532, 2.54771, 2.56141, 2.54771, 2.49408, 2.38239, 1.8731, 0.92514, 0.256859, 0.10316, 0.0937275, 0.0660625, 0.059677, 0.0660625, 0.0656212}

imgx = {-1.064, -0.912, -0.76, -0.608, -0.456, -0.304, -0.152, 0., 0.152, 0.304, 0.456, 0.608, 0.76, 0.912, 1.064, -1.064, -0.912, -0.76, -0.608, -0.456, -0.304, -0.152, 0., 0.152, 0.304, 0.456, 0.608, 0.76, 0.912, 1.064, -1.064, -0.912, -0.76, -0.608, -0.456, -0.304, -0.152, 0., 0.152, 0.304, 0.456, 0.608, 0.76, 0.912, 1.064, -1.064, -0.912, -0.76, -0.608, -0.456, -0.304, -0.152, 0., 0.152, 0.304, 0.456, 0.608, 0.76, 0.912, 1.064, -1.064, -0.912, -0.76, -0.608, -0.456, -0.304, -0.152, 0., 0.152, 0.304, 0.456, 0.608, 0.76, 0.912, 1.064, -1.064, -0.912, -0.76, -0.608, -0.456, -0.304, -0.152, 0., 0.152, 0.304, 0.456, 0.608, 0.76, 0.912, 1.064, -1.064, -0.912, -0.76, -0.608, -0.456, -0.304, -0.152, 0., 0.152, 0.304, 0.456, 0.608, 0.76, 0.912, 1.064, -1.064, -0.912, -0.76, -0.608, -0.456, -0.304, -0.152, 0., 0.152, 0.304, 0.456, 0.608, 0.76, 0.912, 1.064, -1.064, -0.912, -0.76, -0.608, -0.456, -0.304, -0.152, 0., 0.152, 0.304, 0.456, 0.608, 0.76, 0.912, 1.064, -1.064, -0.912, -0.76, -0.608, -0.456, -0.304, -0.152, 0., 0.152, 0.304, 0.456, 0.608, 0.76, 0.912, 1.064, -1.064, -0.912, -0.76, -0.608, -0.456, -0.304, -0.152, 0., 0.152, 0.304, 0.456, 0.608, 0.76, 0.912, 1.064, -1.064, -0.912, -0.76, -0.608, -0.456, -0.304, -0.152, 0., 0.152, 0.304, 0.456, 0.608, 0.76, 0.912, 1.064, -1.064, -0.912, -0.76, -0.608, -0.456, -0.304, -0.152, 0., 0.152, 0.304, 0.456, 0.608, 0.76, 0.912, 1.064, -1.064, -0.912, -0.76, -0.608, -0.456, -0.304, -0.152, 0., 0.152, 0.304, 0.456, 0.608, 0.76, 0.912, 1.064, -1.064, -0.912, -0.76, -0.608, -0.456, -0.304, -0.152, 0., 0.152, 0.304, 0.456, 0.608, 0.76, 0.912, 1.064}

imgy = {-1.064, -1.064, -1.064, -1.064, -1.064, -1.064, -1.064, -1.064, -1.064, -1.064, -1.064, -1.064, -1.064, -1.064, -1.064, -0.912, -0.912, -0.912, -0.912, -0.912, -0.912, -0.912, -0.912, -0.912, -0.912, -0.912, -0.912, -0.912, -0.912, -0.912, -0.76, -0.76, -0.76, -0.76, -0.76, -0.76, -0.76, -0.76, -0.76, -0.76, -0.76, -0.76, -0.76, -0.76, -0.76, -0.608, -0.608, -0.608, -0.608, -0.608, -0.608, -0.608, -0.608, -0.608, -0.608, -0.608, -0.608, -0.608, -0.608, -0.608, -0.456, -0.456, -0.456, -0.456, -0.456, -0.456, -0.456, -0.456, -0.456, -0.456, -0.456, -0.456, -0.456, -0.456, -0.456, -0.304, -0.304, -0.304, -0.304, -0.304, -0.304, -0.304, -0.304, -0.304, -0.304, -0.304, -0.304, -0.304, -0.304, -0.304, -0.152, -0.152, -0.152, -0.152, -0.152, -0.152, -0.152, -0.152, -0.152, -0.152, -0.152, -0.152, -0.152, -0.152, -0.152, 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.152, 0.152, 0.152, 0.152, 0.152, 0.152, 0.152, 0.152, 0.152, 0.152, 0.152, 0.152, 0.152, 0.152, 0.152, 0.304, 0.304, 0.304, 0.304, 0.304, 0.304, 0.304, 0.304, 0.304, 0.304, 0.304, 0.304, 0.304, 0.304, 0.304, 0.456, 0.456, 0.456, 0.456, 0.456, 0.456, 0.456, 0.456, 0.456, 0.456, 0.456, 0.456, 0.456, 0.456, 0.456, 0.608, 0.608, 0.608, 0.608, 0.608, 0.608, 0.608, 0.608, 0.608, 0.608, 0.608, 0.608, 0.608, 0.608, 0.608, 0.76, 0.76, 0.76, 0.76, 0.76, 0.76, 0.76, 0.76, 0.76, 0.76, 0.76, 0.76, 0.76, 0.76, 0.76, 0.912, 0.912, 0.912, 0.912, 0.912, 0.912, 0.912, 0.912, 0.912, 0.912, 0.912, 0.912, 0.912, 0.912, 0.912, 1.064, 1.064, 1.064, 1.064, 1.064, 1.064, 1.064, 1.064, 1.064, 1.064, 1.064, 1.064, 1.064, 1.064, 1.064}

Imgdat = Transpose[{imgx, imgy, img}];

FindFit[Imgdat, c - c Erf[xx - yy Tan[a] - b], {a, b, c}, {xx, yy}]
Out[5]= {a -> -0.0108129, b -> -0.313021, c -> 1.75671}

Show[Plot3D[c - c Erf[xx - yy Tan[a] - b] /. {a -> -0.0108129, b -> -0.313021, 
c -> 1.75671}, {xx, -1.064, 1.064}, {yy, -1.064, 1.064}], Graphics3D[{Red, Point /@ Imgdat}]]

enter image description here

However, the fitted function does not fit the data well enough. It can be seen from the graph that the fitted function is not steep enough. What can be changed in this model(equation) to have a better fit?

Edit:

The above problem is solved by the answer given by george2079 but when I perform a change of basis using the transformation in equation $(1)$ and set $\phi = -a $ in order to make the fitted edge parallel to $y$-axis, the edge doesn't become parallel to the $y$-axis.

$$\begin{bmatrix}xx\\yy\\\end{bmatrix} = \begin{bmatrix}cos\phi&-sin\phi\\sin\phi&cos\phi\\\end{bmatrix} \begin{bmatrix}x\\y\\\end{bmatrix} \tag{1}$$

Model =  c - c Erf[d (x Cos[\[Phi]] - y Sin[\[Phi]]) - (y Cos[\[Phi]] + x Sin[\[Phi]]) Tan[a] - b] 
/. {a -> -0.08322858326935824`, b -> -0.13093494354096177`, c -> 1.2770550170426513`, d -> 4.198092839800464`};

Show[Plot3D[1.2770550170426513` - 1.2770550170426513` Erf[ 0.13093494354096177` 
+ 0.08342129196156162` (y Cos[\[Phi]] + x Sin[\[Phi]]) + 4.198092839800464` (x Cos[\[Phi]] - y Sin[\[Phi]])] 
/. {\[Phi] -> 0.08322858326935824`}, {x, -1.064`, 1.064`}, {y, -1.064`, 1.064`}], 
Graphics3D[{Red, Point /@ Imgdat}]]

However, if I remove the parameter $d$ from the model or set $d=1$, I am able to use $\phi = -a$ to in order to make edge parallel to $y$-axis but, then I end up with the same problem of a bad fit. This can be seen from the code below:

Show[Plot3D[1.2770550170426513` - 1.2770550170426513` Erf[ 0.13093494354096177` 
+ x Cos[\[Phi]] - y Sin[\[Phi]] + 0.08342129196156162` (y Cos[\[Phi]] + x Sin[\[Phi]])]
/. {\[Phi] -> 0.08322858326935824`}, {x, -1.064`, 1.064`}, {y, -1.064`, 1.064`}], Graphics3D[{Red, Point /@ Imgdat}]]

Question: how do I relate $\phi$ with $a$ in the model given by equation (2) so that I can make the edge parallel to the $y$-axis?. $$c - c*Erf[d (xcosϕ−ysinϕ) - (ycosϕ+xsinϕ)*Tan[a] - b] \tag{2}$$

One possible relation that is probably working in this case is $\phi = -a/d$. But this is not correct.

Update:

I tried a bad workaround, first I fit the model equation (2) to my data with $d=1$ and I use the value of parameter $a$ from this operation and set $\phi = -a$:

FindFit[Imgdat, c - c Erf[ xx - yy Tan[a] - b], {a, b, c, d}, {xx, yy}]
Out[]= {a -> -0.0108129, b -> -0.313021, c -> 1.75671, d -> 1.}

Phi = -a /. {a -> -0.010812878221309214`}
Out[]= 0.0108129

Then I fit my data again with the model equation (2), this time without setting any value for the parameter $d$:

FindFit[Imgdat, c - c Erf[ d xx - yy Tan[a] - b], {a, b, c, d}, {xx, yy}]
Out[]= {a -> -0.0832286, b -> -0.130935, c -> 1.27706, d -> 4.19809}  

Finally, I apply the change of basis and set the value of $\phi$ as obtained before:

Plot3D[c - c Erf[d (x Cos[\[Phi]] - y Sin[\[Phi]]) - (y Cos[\[Phi]] + x Sin[\[Phi]]) Tan[a] - b] 
/. {a -> -0.0832286, b -> -0.130935, c -> 1.27706, d -> 4.19809} 
/. {\[Phi] -> Phi}, {x, -1.064, 1.064}, {y, -1.064, 1.064}] 
$\endgroup$
  • 1
    $\begingroup$ You need a parameter multiplying xx. Also since a appears only in Tan[a] you should just make that the parameter, (eg ` Erf[d xx - yy tana - b] `) $\endgroup$ – george2079 Mar 4 '18 at 15:29
4
$\begingroup$

The main issue is you need a parameter multiplying xx to get a good fit. I'd also simplify out the Tan, and use NonLinearModelFit as it has a nicer interface.

ft = NonlinearModelFit[Imgdat, 
   c - c Erf[d xx - yy tana - b], {tana, b, c, d}, {xx, yy}];
Show[Plot3D[ft[xx, yy], {xx, -1.064, 1.064}, {yy, -1.064, 1.064}], 
 Graphics3D[{Red, Point /@ Imgdat}]]

enter image description here

ft["BestFitParameters"]
ArcTan[tana /. %]

{tana -> -0.0834207, b -> -0.130935, c -> 1.27706, d -> 4.1981}

-0.083228

$\endgroup$
  • $\begingroup$ c - c Erf[d xx - yy Tan[a] - b] is working good for this case but I wanted to perform a change of basis by using $xx= x*cos\phi - y*sin\phi$ and $yy = y*cos\phi + x*sin\phi$ so that I can use $\phi = -a$ in order to get the edge parallel to $y-axis$. Without the $d$ parameter I am able to do this by using $\phi = -a$ but, with the $d$ parameter I need a different relation between $\phi$ and $a$ what could be that?. $\endgroup$ – dykes Mar 6 '18 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.