Filter löschen
Filter löschen

The graph basic fitting tool accepts a matrice but not the polyfit function... How to do the same thing in a script ?

1 Ansicht (letzte 30 Tage)
I made a graph of the evolution of a variable through 10 time periods (colums) for 10 individuals (lines). The graph basic fitting gives a nice fourth degree fit which I want to reproduce in a script for different variables. I tried with polyfit but it doe not take a matrice as input. Could somebody tell me how to replicate the treatment of the basic fittiing tool in a script ? Here is the graph and the data in DATA.mat attached file.
Thanks alot for any help.

Akzeptierte Antwort

Image Analyst
Image Analyst am 28 Mär. 2023
Try this:
% Optional initialization steps
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
% Load user's data.
s = load('DATA10Curves.mat')
s = struct with fields: vcz: [10×10 double]
vcz = s.vcz;
[rows, columns] = size(vcz)
rows = 10
columns = 10
% Make dat for all of them
xAll = zeros(1, rows * columns);
yAll = zeros(1, rows * columns);
% Scan each person's data adding them to a master array.
for row = 1 : rows
% Timepoints for an individual are in columns,
% so one row is all the times for one individual.
y = vcz(row, :);
% Polyfit can't take multiple y values for the same x value,
% so let's add a very very small amount of noise to the x values.
% It won't be enough to affect the fit.
smallNoise = 0.001 * (rand(1, columns) - 0.5); % Zero mean noise.
x = (1 : columns) + smallNoise;
plot(x, y, '.-', 'LineWidth', 2, 'MarkerSize', 15);
grid on;
hold on;
% Store in our master array.
startingIndex = (row - 1) * columns + 1;
endingIndex = startingIndex + columns - 1;
xAll(startingIndex : endingIndex) = x;
yAll(startingIndex : endingIndex) = y;
% It's not needed but let's sort by x anyway.
[xAll, sortOrder] = sort(xAll, 'ascend');
yAll = yAll(sortOrder);
% Fit to a 4th order equation.
coefficients = polyfit(x, y, 4);
% Get fitted values at the 10 original x points.
xFit = 1 : columns;
yFit = polyval(coefficients, xFit);
% Plot fitted line in black over the other curves.
plot(xFit, yFit, 'k-', 'LineWidth', 5);
hold off
xlabel('x', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);

Weitere Antworten (1)

Antoni Garcia-Herreros
Antoni Garcia-Herreros am 27 Mär. 2023
Something like this might do the trick:
close all
for i=1:10; % Loop through your variables
hold on
L.String{end}=['Fitted ' L.String{end-1}];
Hope this helps!
  1 Kommentar
Jacques Larue
Jacques Larue am 27 Mär. 2023
Thanks a lot for your answer. Your code allowed me to identify that the Basic fiffting tool use only the first line of the matrice. I thought that somehow the tool would have taken into account the entire matrice.

Melden Sie sich an, um zu kommentieren.


Mehr zu Mathematics finden Sie in Help Center und File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by