Matrix containing results of polynomial function

조회 수: 2(최근 30일)
paul 2022년 9월 5일
Moved: Steven Lord 2022년 9월 6일
Hi,
I'm trying to create a matrix where some of the elements are the results of a polynomial function, the coefficients of which are taken from another matrix:
series_fit = [series(1,1) series(1,2) series(1,3) series(1,4) series(1,5) series(1,6);
(A(3,1)*series(1,1)^2)+(A(2,1)*series(1,1))+(A(1,1)*series(1,1))
(A(3,1)*series(1,2)^2)+(A(2,1)*series(1,2))+(A(1,1)*series(1,2))
(A(3,1)*series(1,3)^2)+(A(2,1)*series(1,3))+(A(1,1)*series(1,3))
(A(3,1)*series(1,4)^2)+(A(2,1)*series(1,4))+(A(1,1)*series(1,4))
(A(3,1)*series(1,5)^2)+(A(2,1)*series(1,5))+(A(1,1)*series(1,5))
(A(3,1)*series(1,6)^2)+(A(2,1)*series(1,6))+(A(1,1)*series(1,6))]
Matrix series is 2 x 6 containing x and y data pairs. Matrix A is 3 x 1 containing three polynomial coefficients. I'm trying to create a matrix I can pass to the PLOT() function, but I get the followinge error at the line above:
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in tutorial_5a (line 38)
series_fit = [series(1,1) series(1,2) series(1,3) series(1,4) series(1,5) series(1,6);
I am very certain that this is not the best way to achieve what I'm trying to do, so any advice or guidance would be appreciated - I'm very muchfiguring this out as I go...
Thanks for any help :)

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

답변(1개)

William Rose 2022년 9월 5일
편집: William Rose 2022년 9월 5일
I assume you have 6 (x,y) pairs and a quadratic fit. Your equations show A(2,1) and A(1,1) both multiplying series(1,:). I suspect that is a mistake andthat the actual fitting equation is
yfit = a1 + a2*x + a3*x^2
Define the coefficients:
a=[-1,2,1];
Make some sample data,with random noise added:
x=1:6;
series=[x;a*[x.^0;x;x.^2]+randn(1,6)]
series = 2×6
1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 1.6927 9.5552 15.2512 24.7072 33.6129 46.9532
Computed the fitted values:
yfit=a*[x.^0;x;x.^2]
yfit = 1×6
2 7 14 23 34 47
Plot results:
plot(series(1,:),series(2,:),'*r',x,yfit,'-r');
Try it.
댓글 수: 2표시숨기기 이전 댓글 수: 1
paul 2022년 9월 6일
Moved: Steven Lord 2022년 9월 6일
Hello,
I've figured out what I'm doing wrong - it was a syntax error!
To split long lines across multiple lines, you apparrently need to use the "ellipsis", i.e. three dots.
Like I said, I'm figuring this out as I go, but the working syntax for the above is:
% create a series for the fit-line
series_fit = [series(1,1) series(1,2) series(1,3) series(1,4) series(1,5) series(1,6); ...
(A(3,1)*series_x2(1,1))+(A(2,1)*series(1,1))+(A(1,1)) ...
(A(3,1)*series_x2(1,2))+(A(2,1)*series(1,2))+(A(1,1)) ...
(A(3,1)*series_x2(1,3))+(A(2,1)*series(1,3))+(A(1,1)) ...
(A(3,1)*series_x2(1,4))+(A(2,1)*series(1,4))+(A(1,1)) ...
(A(3,1)*series_x2(1,5))+(A(2,1)*series(1,5))+(A(1,1)) ...
(A(3,1)*series_x2(1,6))+(A(2,1)*series(1,6))+(A(1,1))]
Paul

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

범주

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by