Creating a manual fit to a plot

7 views (last 30 days)
Still_i_Rise
Still_i_Rise on 8 Mar 2024
Commented: Harald on 10 Mar 2024
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 Comments
Still_i_Rise
Still_i_Rise on 8 Mar 2024
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 on 9 Mar 2024
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

Sign in to comment.

Answers (1)

Harald
Harald on 8 Mar 2024
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 Comments
Still_i_Rise
Still_i_Rise on 10 Mar 2024
Edited: Still_i_Rise on 10 Mar 2024
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 on 10 Mar 2024
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

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!