Fminsearch curve fitting does not fit properly

5 ビュー (過去 30 日間)
Jonas Gentner
Jonas Gentner 2021 年 11 月 19 日
コメント済み: Jonas Gentner 2021 年 11 月 20 日
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.

採用された回答

Matt J
Matt J 2021 年 11 月 19 日
編集済み: Matt J 2021 年 11 月 19 日
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 件のコメント
Jonas Gentner
Jonas Gentner 2021 年 11 月 20 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by