I want to calculate m, b using the polyfit function over and over and over again.

4 Ansichten (letzte 30 Tage)
x = randn(1, 100)
y= randn(1, 100)
P= polyfit (x,y,1)
m = P(1)
b = P(2)
In this way, m and b values, which are put into the primary function, will be displayed in the interval between 1 and 100.
But I'm one to five, five to ten, ... I would like to calculate m, b, and draw a scatterplot from 95 to 100.

Akzeptierte Antwort

Image Analyst
Image Analyst am 15 Feb. 2020
Try this, where I plot b and m for all 100 trials, and do a scatterplot for trials 95 through 100, as you asked:
% Initialization steps:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 24;
markerSize = 40;
numberOfTrials = 100;
for k = 1 : numberOfTrials
x = randn(1, 100);
y = randn(1, 100);
% Fit a line.
P = polyfit (x,y,1);
m(k) = P(1);
b(k) = P(2);
fprintf('For trial #%3d of %3d, m = %.4f and b = %.4f.\n', ...
k, numberOfTrials, m(k), b(k));
end
% DONE! Now do the plotting.
% Plot m and b
hFig = figure;
subplot(2, 1, 1);
plot(m, 'b.-', 'LineWidth', 2, 'MarkerSize', markerSize);
grid on;
hold on;
plot(b, 'r.-', 'LineWidth', 2, 'MarkerSize', markerSize);
xlabel('Trial', 'FontSize', fontSize);
ylabel('m and b', 'FontSize', fontSize);
caption = sprintf('b and m for all %d points', numberOfTrials);
title(caption, 'FontSize', fontSize);
legend('m', 'b');
% Make a scatterplot of b vs. m but just for the indexes 95 to 100 (for some reason).
subplot(2, 1, 2);
scatter(m(95:100), b(95:100), markerSize, 'filled');
grid on;
xlabel('m', 'FontSize', fontSize);
ylabel('b', 'FontSize', fontSize);
title('b vs. m for the last 6 points (index 95 - 100)', 'FontSize', fontSize);
hFig.WindowState = 'maximized';
  6 Kommentare
Image Analyst
Image Analyst am 18 Feb. 2020
Unfortunately you forgot to attach '20200207_1.xlsx', so now, when I had time to look at it, I can't because you didn't upload the workbook. I'll check back later for it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by