Creating a manual fit to a plot
Afficher commentaires plus anciens
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 commentaires
Mathieu NOE
le 8 Mar 2024
undefined total_freq
Still_i_Rise
le 8 Mar 2024
Harald
le 8 Mar 2024
A best practice when asking a question is to include all information, including data, required to run the code.
Still_i_Rise
le 8 Mar 2024
Harald
le 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
Réponses (1)
Harald
le 8 Mar 2024
0 votes
Hi,
one way to do that sort of "manual fit" would be to use a slider or comparable interactive component in Live Editor.
For more details on this, please see https://www.mathworks.com/help/matlab/matlab_prog/add-interactive-controls-to-a-live-script.html
However, the intention may be that you write an own simple optimization algorithm to automatically find the best kn.
Best wishes,
Harald
7 commentaires
Still_i_Rise
le 9 Mar 2024
Harald
le 9 Mar 2024
The question then to me is why you are not supposed to use the fit function.
If it's just a preference not to, you could use other suitable functions such as lsqcurvefit (Optimization Toolbox).
If the intention is to avoid toolbox functionality, you can use fminsearch or fminbnd to minimize the summed squares of the deviations. There are a lot of related answers, e.g.
Best wishes,
Harald
Still_i_Rise
le 9 Mar 2024
Harald
le 9 Mar 2024
Hi,
This information is very helpful. Next time, please specify it from the beginning.
If CFT is not giving the right fit, the reason is typically starting values that are not good enough. If you provide a starting value that is reasonably close to the solution, you will often get better results.
To further assist you with this, a reproducible example will be needed.
Best wishes,
Harald
Harald
le 9 Mar 2024
Additionally, have you tried the other options I have suggested?
Best wishes,
Harald
Still_i_Rise
le 10 Mar 2024
Modifié(e) : Still_i_Rise
le 10 Mar 2024
Harald
le 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
Catégories
En savoir plus sur Linear Least Squares dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!