Taking elements of matrix into coefficient of a function
4 views (last 30 days)
Show older comments
Satria Indrawan
on 27 Sep 2021
Answered: Star Strider
on 27 Sep 2021
I have a 1x3 matrix. I want to make each elements of the matrix into coefficients of a quadratic function.
Say the matrix is
A = [c1 c2 c3]
I want to make a function, for example
q = c1+c2.*x+c3.*x.^2
with x for example:
x = 1:1e-2:2
with c1, c2, and c3 are all the elements of the matrix A.
When I make a function of it, it says the dimensions are incorrect. I get why it's incorrect, I just want to know how to do it.
0 Comments
Accepted Answer
Star Strider
on 27 Sep 2021
Try this —
A = [randi(9) randi(9) randi(9)]
x = 1:1e-2:2;
y = polyval(flip(A), x);
figure
plot(x, y)
grid
The polyval function evaluates ‘A’ as ‘A(3)*x.^2 + A(2)*x + A(1)’ so it is necessary to flip it in the polyval call.
.
0 Comments
More Answers (0)
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!