Creating a manual fit to a plot

조회 수: 5 (최근 30일)
Still_i_Rise
Still_i_Rise 2024년 3월 8일
댓글: Harald 2024년 3월 10일
i have a frequency data and amplitude data. i have to figure out the stiffness with the help of the resonance frequency of an freely damped oscillator. i have been asked to fit the data manually without using fit function. how can i do that ? this is my code. my plan was to create 2 plots: 1 with the ideal data and another with the constants given in the equation. and the match the graphs by changing the value of kn. as other values are known and constant
T = 300;
Q = 384.61;
f_n = 62460;
kB = 1.380648520000000e-23;
kn = 20; % initial guess
total_d_z = 1.095866243071940e-20;
frequency = linspace(total_freq(1000), total_freq(6300), 54001);
d_tot = normalized_psd(900:0.1:6300);
x = frequency;
% Define the function y
y = @(x, kn)((((2 * kB * T)./(pi * kn * f_n * Q))./((x.^2./((f_n^2)-1)).^2 + (x./(f_n^2*Q)).^2) + total_d_z^2));
% Plot the data
plot(x, d_tot, 'DisplayName', 'Measured Data', 'LineWidth', 1);
hold on;
%Plot the initial model with the initial guess for kn
plot(x,y(x, kn), 'LineWidth', 1, 'DisplayName', ['Initial Model (kn=' num2str(kn_initial) ')'],'Marker','+');
  댓글 수: 5
Still_i_Rise
Still_i_Rise 2024년 3월 8일
Hello !
Sorry, i am new here, thanks for the suggestion, i'll remember this for future. My data is in around 60k samples which is why i didnt upload it.
Harald
Harald 2024년 3월 9일
No worries.
Often times, the full data is not needed - just a sample that sufficiently shows what's going on.
Just wanting to make sure: have you noticed the answer below?
Best wishes,
Harald

댓글을 달려면 로그인하십시오.

답변 (1개)

Harald
Harald 2024년 3월 8일
Hi,
one way to do that sort of "manual fit" would be to use a slider or comparable interactive component in Live Editor.
However, the intention may be that you write an own simple optimization algorithm to automatically find the best kn.
Best wishes,
Harald
  댓글 수: 7
Still_i_Rise
Still_i_Rise 2024년 3월 10일
편집: Still_i_Rise 2024년 3월 10일
Hi Herald,
really appriciate your help. Yes i did try other options. lsqcurvefit says
Optimization completed because the size of the gradient is less than
the value of the optimality tolerance.
Optimization completed: The first-order optimality measure, 2.917292e-27,
is less than options.OptimalityTolerance = 1.000000e-06.
##### With fminsearch :
i am trying to use fminsearch with the frequency vector but its bit hard for me right now. See the code below.
the reason why i am using total_freq, total_freq1 and frequency because matlab says its an unknown variable. so i was just trying to convince it. I already have my frequency data as total_freq. here i am trying to estimate 2 values one is kn and another is Q which is supposed to be 384
%% With fminsearch
x0 = [20 ; 384.61];
fmin_result = fminsearch(@Stiffness,x0);
total_d_z = 2.190266219650838e-21;
function [f] = Stiffness(x)
frequency = linspace(total_freq1(1000), total_freq1(2621), 345);
total_freq1 = total_freq;
T = 300;
f_n = 62460;
kB = 1.38e-23;
f = ((((2 * kB *T)/(pi* x(1) * f_n * x(2))) ./ ( ((frequency.^2)./(f_n^2-1)).^2 + (frequency ./(f_n^2)*x(2)).^2 )) + total_d_z^2);
end
Harald
Harald 2024년 3월 10일
Even if the optimality measures look good, this might only be a local minimum, not the global minimum that you are looking for.
To use fminsearch, you need to use an anonymous function handle, see the example "Minimize with Extra Parameters" in the doc of fminsearch.
Again, please share sample data for further assistance.
Best wishes,
Harald

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by