Polyfit matrix from import TXT file

5 visualizzazioni (ultimi 30 giorni)
Tate Kafka
Tate Kafka il 6 Set 2022
Commentato: Rik il 7 Set 2022
Hi, I am trying to import a TXT file containing 100 rows x 2 columns of data. I then want to take that data and polyfit it. However, what I have been trying so far receives the errror: Warning: Polynomial is not unique; degree >= number of data points. All of my matrices come out with correct data.
My current code:
% Import data
filename = 'A2Q2_input_a.txt';
delimiterIn = ' ';
headerlinesIn = 1;
A = importdata(filename);
for k = [1:100]
D = (A.data(k, :));
domain(k) = D(1));
percent_err(k) = D(2);
% perform linear polyfit function
p_linear_R4 = polyfit(domain,percent_err,1);
  3 Commenti
Tate Kafka
Tate Kafka il 7 Set 2022
Yes i would like a single polynomial to be fitted to the entire matrix
Rik
Rik il 7 Set 2022
Then the answer posted by KSSV should be what you need. If not, attach the file to your question and explain why it doesn't work.

Accedi per commentare.

Risposte (1)

KSSV
KSSV il 6 Set 2022
% Import data
filename = 'A2Q2_input_a.txt';
T = readtable(filename);
x = T.(1) ;
y = T.(2) ;
% perform linear polyfit function
p_linear_R4 = polyfit(x,y,1);
yi = polyval(p_linear_R4,x) ;
figure
hold on
plot(x,y,'.b')
plot(x,yi,'r')

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by