Problem using lsqnonlin for curve fitting
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
I am trying to use lsqnonlin to match my measured field signal with the plot generated from my objective function. The problem I am facing is that the objective function is a little bit complicated:
%% OBJECTIVE FUNCTION %% function [H] = objective_function(Pos)
MeasPoint_X = Pos(1); MeasPoint_Y = Pos(2); MeasPoint_Z = Pos(3);
Vertix1 = [0.15 0.15 0]; Vertix2 = [0.15 -0.15 0]; Vertix3 = [-0.15 -0.15 0]; Vertix4 = [-0.15 0.15 0]; Current = 1;
MeasPoint = []; H = []; for MeasPoint_X = 1:1:length(MeasPoint_X) MeasPoint_temp = [MeasPoint_X MeasPoint_Y MeasPoint_Z]; H_temp = BiotSavartCalculation(Vertix1, Vertix2, Vertix3, Vertix4,... MeasPoint_temp, Current); MeasPoint = [MeasPoint; MeasPoint_temp]; H = [H; H_temp]; end end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% BIOT SAVART CALCULATION %% This uses another function to calculate the magnetic field along a certain trajectory:
function [H, B, x, y, z] = BiotSavartCalculation(Vertix1, Vertix2, Vertix3, Vertix4,... MeasPoint, Current)
x = [Vertix1(1) Vertix2(1) Vertix3(1) Vertix4(1) Vertix1(1)]; y = [Vertix1(2) Vertix2(2) Vertix3(2) Vertix4(2) Vertix1(2)]; z = [Vertix1(3) Vertix2(3) Vertix3(3) Vertix4(3) Vertix1(3)];
% Define the sides of the coil Side1 = Vertix2 - Vertix1; Side2 = Vertix3 - Vertix2; Side3 = Vertix4 - Vertix3; Side4 = Vertix1 - Vertix4;
% Side 1: A1 = MeasPoint - Vertix2; B1 = MeasPoint - Vertix1; % Side 2: A2 = MeasPoint - Vertix3; B2 = MeasPoint - Vertix2; % Side 3: A3 = MeasPoint - Vertix4; B3 = MeasPoint - Vertix3; % Side 4: A4 = MeasPoint - Vertix1; B4 = MeasPoint - Vertix4;
% Implementation of Biot-Savart Law:
u = 1.25663706*(10^-6); % Permeability of free space
H1 = ((dot(Side1,B1)/norm(B1)) - (dot(Side1,A1)/norm(A1)))*(cross(A1,B1)/(norm(cross(A1,B1))^2)); H2 = ((dot(Side2,B2)/norm(B2)) - (dot(Side2,A2)/norm(A2)))*(cross(A2,B2)/(norm(cross(A2,B2))^2)); H3 = ((dot(Side3,B3)/norm(B3)) - (dot(Side3,A3)/norm(A3)))*(cross(A3,B3)/(norm(cross(A3,B3))^2)); H4 = ((dot(Side4,B4)/norm(B4)) - (dot(Side4,A4)/norm(A4)))*(cross(A4,B4)/(norm(cross(A4,B4))^2));
H = (1/4*pi)*(H1+H2+H3+H4); B = u*Current*H; end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The problem occurs when trying to match the measured data with the simulated data using the levenberg-marquadt method, lsqnonline. This is the code I wrote for calling the LM solver:
clc; clear all;
MeasPoint_X = 1:1:100; MeasPoint_Y = 50; MeasPoint_Z = 10; Pos = [MeasPoint_X MeasPoint_Y MeasPoint_Z];
[H] = objective_function(Pos); H_i_sim = H(:,1); H_i_meas = H(:,1) + (10^-8.5)*randn(100,1);
% plot(MeasPoint_X, H_i_sim, MeasPoint_X, H_i_meas) % xlabel('X-distance (cm)') % ylabel('Magnitude A/m)') X0 = [0 0 0];
x = lsqnonlin(@objective_function, X0);
I am struggling to implement the LM solver for my application. I understand the method iterates to find the optimum value of the coefficients but wanted to know if that can be applied to my case.
Many thanks,
Mo
0 Kommentare
Antworten (1)
Star Strider
am 23 Apr. 2014
Use lsqcurvefit instead. It is specifically designed for curve-fitting. You will also need to change your objective function. See the section on fun in the Input Arguments section for details. It requires the objective function arguments to be in a specific order.
0 Kommentare
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!