How to debug this? Not getting into it. Need help
Show older comments
% Data
rData = [5.3571429, 0.096854535; 10.714286, 0.055104186; 16.071429, 0.042811499; 21.428571, 0.024825886; 26.785714, 0.023279183; 32.142857, 0.016328542; 37.5, 0.0092185037; 42.857143, 0.0075624777; 48.214286, 0.0023514323; 53.571429, 0.001637045; 58.928571, -0.0024887011; 64.285714, -0.0034741333; 69.642857, -0.0056340032; 75, -0.0040906991; 80.357143, -0.0039738424; 85.714286, -0.0044593789; 91.071429, -0.0054884315; 96.428571, -0.0037277341; 101.78571, -0.0041691748; 107.14286, -0.0039292558; 112.5, -0.0037408923; 117.85714, -0.0040700255; 123.21429, -0.0028904555; 128.57143, -0.0022557232; 133.92857, -0.0020756487; 139.28571, -0.0020739949; 144.64286, -0.0015149035; 150, -0.0019796368; 155.35714, -0.00068430865; 160.71429, -0.00060721168; 166.07143, -0.00055972397; 171.42857, -0.0011788755; 176.78571, -0.00090675531; 182.14286, -0.00060012026; 187.5, 7.6071311e-6];
tData = [5.3571429, 0.081473653; 10.714286, -0.0076210718; 16.071429, -0.038565046; 21.428571, -0.014000405; 26.785714, -0.042161254; 32.142857, -0.071404281; 37.5, -0.066992712; 42.857143, -0.031355057; 48.214286, -0.02043848; 53.571429, -0.025259291; 58.928571, -0.019615094; 64.285714, -0.015185751; 69.642857, -0.012213914; 75, -0.0047624032; 80.357143, -0.00041652762; 85.714286, 0.0028162852; 91.071429, 0.00979253; 96.428571, 0.0080315783; 101.78571, 0.0034739882; 107.14286, 0.0021786814; 112.5, 0.0043349925; 117.85714, 0.0053397331; 123.21429, 0.0061087654; 128.57143, 0.0028425693; 133.92857, 0.002129577; 139.28571, 0.0068534431; 144.64286, 0.0071201038; 150, 0.0099290536; 155.35714, 0.0089545127; 160.71429, 0.0079282308; 166.07143, 0.0075533041; 171.42857, 0.01092774; 176.78571, 0.012219652; 182.14286, 0.01013098; 187.5, 0.0096622622];
% Define equations
Alpha = @(mu, lambda) 1/(2*mu + lambda);
% Define equations
eqns = @(x, y, mu, lambda, ke, ko) [y(2); -y(1) + Alpha(mu, lambda)^-1 * ke^2 * x.^2 .* y(1) - Alpha(mu, lambda)^-1 * ko^2 * x.^2 .* y(2)];
% Define the model
funr = @(params, x) deval(ode45(@(x, y) eqns(x, y, params(1), params(2), params(3), params(4)), [0.75, 187.5], [0.1625, 0]), x, 1);
% Fit the data with initial guess values
initialGuess = [15, 50, 0.01, 0.01];
fit = lsqcurvefit(@(params, x) funr(params, x), initialGuess, rData(:,1), rData(:,2));
fit2 = lsqcurvefit(@(params, x) funr(params, x), initialGuess, tData(:,1), tData(:,2));
% Plot the fitted functions
x_values = linspace(1, 187.5, 1000);
r_fit = funr(fit, x_values);
theta_fit = funr(fit2, x_values);
figure;
plot(x_values, r_fit, 'r', 'LineWidth', 2);
hold on;
scatter(rData(:,1), rData(:,2), 'b');
xlabel('x');
ylabel('r(x)');
title('Fitted r(x)');
legend('Fitted r(x)', 'rData');
figure;
plot(x_values, theta_fit, 'r', 'LineWidth', 2);
hold on;
scatter(tData(:,1), tData(:,2), 'b');
xlabel('x');
ylabel('\theta(x)');
title('Fitted \theta(x)');
legend('Fitted \theta(x)', 'tData');
~
Getting the following Errors: using lsqcurvefit Function value and YDATA sizes are not equal. Error in test (line 16) fit = lsqcurvefit(@(params, x) deval(funr(params(1), params(2), params(3), params(4), x), x, 1), initialGuess, rData(:,1), rData(:,2));
Answers (1)
Torsten
on 4 Mar 2024
Use
fit = lsqcurvefit(@(params, x) funr(params, x), initialGuess, rData(:,1).', rData(:,2).');
fit2 = lsqcurvefit(@(params, x) funr(params, x), initialGuess, tData(:,1).', tData(:,2).');
instead of
fit = lsqcurvefit(@(params, x) funr(params, x), initialGuess, rData(:,1), rData(:,2));
fit2 = lsqcurvefit(@(params, x) funr(params, x), initialGuess, tData(:,1), tData(:,2));
Categories
Find more on Get Started with Curve Fitting Toolbox 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!