Matrix containing results of polynomial function
3 views (last 30 days)
Show older comments
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 :)
0 Comments
Answers (1)
William Rose
on 5 Sep 2022
Edited: William Rose
on 5 Sep 2022
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)]
Computed the fitted values:
yfit=a*[x.^0;x;x.^2]
Plot results:
plot(series(1,:),series(2,:),'*r',x,yfit,'-r');
Try it.
2 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!