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

조회 수: 1 (최근 30일)
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.

채택된 답변

Image Analyst
Image Analyst 2023년 3월 28일
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;
end
% 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);

추가 답변 (1개)

Antoni Garcia-Herreros
Antoni Garcia-Herreros 2023년 3월 27일
Hello,
Something like this might do the trick:
close all
for i=1:10; % Loop through your variables
p=polyfit([1:10]',vcz(:,i)',4);
YT=polyval(p,T);
p1=plot([1:10],vcz(:,i));
L=legend;
hold on
p2=plot(T,YT,'LineWidth',2);
L.String{end}=['Fitted ' L.String{end-1}];
end
Hope this helps!
  댓글 수: 1
Jacques Larue
Jacques Larue 2023년 3월 27일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by