Fminsearch curve fitting does not fit properly
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Jonas Gentner
el 19 de Nov. de 2021
Comentada: Jonas Gentner
el 20 de Nov. de 2021
Hi,
i am trying to fit a simple nonlinear pendulum model to measured data by adjusting the damping constant (parameter estimation).
my pendulum function:
function simY = PendelOde2(b,varargin)
u =1;
m = 0.5035; % [kg] mass
l = 0.1315; % [m] length of pendulum
g = 9.81; % [m/s²] gravitational acceleration
expTime = 0:1e-2:20.02;
tic
ODE_Sol = ode45(@(tt,x) myNonlinearPendulum(tt,x,u,m,g,l,b),[0 200.2],[deg2rad(13.5253),deg2rad(0)]);
simY = deval(ODE_Sol, expTime);
simY = simY(1,:);
toc
function [dx,y] = myNonlinearPendulum(t,x,u,m,g,l,b,varargin)
% Output equation.
y = x(1); % Angular position.
% State equations.
dx = [x(2); ... % Angular position
-(g/l)*sin(x(1))-b/(m*l^2)*x(2) ... % Angular velocity
];
end
end
how i am trying to fit (using fminsearch):
clear;
clc;
load('Pendel1_11_eigen.mat')
x = Pendel1_11_eigen.X.Data;
y = Pendel1_11_eigen.Y.Data;
m = 0.5035; % [kg] mass
l = 0.1315; % [m] length of pendulum
g = 9.81; % [m/s²] gravitational acceleration
b0 = 3.1e-4; % [Nm s] damping constant (to estimate)
[bmin, Smin] = fminsearch(@(b) norm(PendelOde2(b) - y), b0)
y1 = rad2deg(PendelOde2(bmin))
plot(x,y)
hold on
plot(x,y1)
grid
This code is running, but the solution of the optimization does not fit to the data (see plot below). At this point i am a little confused why fminsearch thinks it has found a optimal solution and what i should change to make this work.
Any help is highly appreciated. Thanks in advance.
0 comentarios
Respuesta aceptada
Matt J
el 19 de Nov. de 2021
Editada: Matt J
el 19 de Nov. de 2021
Plotting your function in an interval around bmin shows that it is a minimum of the function you've provided. So, fminsearch did its job correctly.
The only thing to assume is either that this is a local minimum (you need a better initial guess), or else it is, indeed, the best fit possible given the model you're using.
[bmin, Smin, ef] = fminsearch(@(b) norm(PendelOde2(b) - y), b0);
fun=@(b) norm(PendelOde2(b) - y);
fun=@(b)arrayfun(fun,b);
t=linspace(-1,1,21);
Ft=fun(bmin+t*b0);
plot(t,Ft)
Más respuestas (0)
Ver también
Categorías
Más información sobre Get Started with Curve Fitting Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!