Fminsearch curve fitting does not fit properly

4 views (last 30 days)
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.

Accepted Answer

Matt J
Matt J on 19 Nov 2021
Edited: Matt J on 19 Nov 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)
  1 Comment
Jonas Gentner
Jonas Gentner on 20 Nov 2021
Thanks for the reply. The initial guess i am making here with b0 is already a really good fit that i found manually. It is way better than what fminsearch is returning as an optimum. It is hard to find a better initial guess and it shows that the model is capable of fitting to this data.
What i found out in my despair, is that fminsearch is fitting perfectly when i change the length of pendulum parameter from 0.1315 to 0.135. I guess i need to check the pendulum length of my test setup again.

Sign in to comment.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!