Matrix containing results of polynomial function

조회 수: 2 (최근 30일)
paul
paul 2022년 9월 5일
이동: 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
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
paul
paul 2022년 9월 6일
이동: Steven Lord 2022년 9월 6일
Hi William,
thanks very much for your help with this.
I've corrected one mistake in the script, where I was accidentally multiplying my thrid coefficient by the series x-value again - too much cutting and pasting!
I'm not using the fit() function, instead I've calculated the coefficients for a second-order polynomial fit line myself, using the cross-product method.
The three coefficients are in matrix A in the form:
A =
-0.1814
-0.3221
1.3537
The data I'm trying to plot a fit-line against is in the matrix series, in the form:
series =
0 0.5000 1.0000 1.5000 2.0000 2.5000
0.0674 -0.9156 1.6253 3.0377 3.3535 7.9409
My hope was that, by doing this...
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))
(A(3,1)*series(1,2)^2)+(A(2,1)*series(1,2))+(A(1,1))
(A(3,1)*series(1,3)^2)+(A(2,1)*series(1,3))+(A(1,1))
(A(3,1)*series(1,4)^2)+(A(2,1)*series(1,4))+(A(1,1))
(A(3,1)*series(1,5)^2)+(A(2,1)*series(1,5))+(A(1,1))
(A(3,1)*series(1,6)^2)+(A(2,1)*series(1,6))+(A(1,1))]
...I would, for example for the first row value in the second column above, be executing the following calculation:
A(3,1) = 1.3537 A(2,1) = -0.3221 A(1,1) = -0.1814
series(1,1) = 0
f(x) = 1.3537 * 0^2 + -0.3221 * 0 + -0.1814
...however, there seems to be a syntax error or similar on this line, as MATLAB appears to think I'm concatenating arrays and have got my delimiters wrong.
Thanks for any further help you can offer,
Paul
paul
paul 2022년 9월 6일
이동: 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))]
...talk about annoying!
Thanks to anybody who was having a think about this for me :)
Paul

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by