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

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

채택된 답변

Image Analyst
Image Analyst 2020년 2월 15일
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
Image Analyst
Image Analyst 2020년 2월 18일
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.
jungmin park
jungmin park 2020년 2월 19일
I forgot to attach the file. Please solve the problem.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by