find the required values using Least square fitting curve tool

1 次查看(过去 30 天)
I have a function f1(r,v,a,q) with all known values (for example f1 = 2,r=3,a=2.5,q = 0.5). I want to estimate the values of r,v,a in a similar function f2(r,v,a,q) (where f2 = 1.5 and q=0.5 is known) using least square curve fitting tool. q always has the same values as within f1.
E = sum(i=1->N)[f2i(qi)-f1i(qi)] this will compute me r,v,a for f2.
could any one help me how to do in matlab? any tutorial or example work would be great help.
thank you

回答(1 个)

Prateekshya
Prateekshya 2024-10-22
Hello Muhammad,
To estimate the values of parameters r, v, and a in your function using least squares curve fitting in MATLAB, you can use optimization tools like lsqcurvefit from the Optimization Toolbox. Below is a step-by-step guide on how to set this up, along with an example.
  • Define Your Functions: You need to have both functions and defined. These should accept parameters r, v, a, and q.
  • Set Up the Objective Function: This function will calculate the difference between and for given parameter values and return the sum of squared differences.
  • Use lsqcurvefit: This function will help find the parameters that minimize the difference between and .
Here is an example of how you might implement this in MATLAB:
% Define the function f1
f1 = @(r, v, a, q) r .* q + v .* q.^2 + a; % Example function definition
% Known values for f1
r_true = 3;
v_true = 1;
a_true = 2.5;
q_values = linspace(0, 1, 10); % Example q values
f1_values = f1(r_true, v_true, a_true, q_values);
% Define the function f2 with parameters to estimate
f2 = @(params, q) params(1) .* q + params(2) .* q.^2 + params(3);
% Target values for f2
f2_target = 1.5 * ones(size(q_values)); % Example target values
% Objective function for least squares
objective = @(params) f2(params, q_values) - f2_target;
% Initial guess for [r, v, a]
initial_guess = [1, 1, 1];
% Perform least squares curve fitting
options = optimoptions('lsqcurvefit', 'Display', 'iter');
estimated_params = lsqcurvefit(f2, initial_guess, q_values, f1_values, [], [], options);
% Display estimated parameters
disp('Estimated Parameters:');
disp(['r = ', num2str(estimated_params(1))]);
disp(['v = ', num2str(estimated_params(2))]);
disp(['a = ', num2str(estimated_params(3))]);
I hope this helps!

类别

Help CenterFile Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by