Julia code to generate the data:
xdata = -2,-1.64,-1.33,-0.7,0,0.45,1.2,1.64,2.32,2.9
ydata = 0.699369,0.700462,0.695354,1.03905,1.97389,2.41143,1.91091,0.919576,-0.730975,-1.42001
and you’d like to fit the function
F(p0,p1,p2,x) = p0+p1*cos(p2*x)+p2sin(p1*x)
using nonlinear least squares.
Use Julia or Matalb to apply the algorithm presented in the lecture notes to solve the nonlinear least-squares example above. (i.e. using the same data and specification form)
You have then to program the different steps instead of using a direct function like “curve_fit”
You will create a loop. (while loop is better here)
For each iteration, you use your estimated values from the previous iteration (or initial guess) to calculate your new estimated value.
You get a new estimated value you can use in the next iteration.
You stop when the distance between two consecutive estimated values is small enough. (you can take the norm of the difference between consecutive estimated vector p)
When you calculate the Jacobian for the two parameters, it is easier to do it creating a matrix . You can call a column of a matrix by using “:”. Each column of will be the Jacobian w.r.t. one parameter.
You should use .* instead of * for element-wise (element by element) multiplication
You only need to estimate the parameters
don’t forget to explain your code with a few comments (comments after ‘#’)