How can I write the following matrix?

if true
A = [x(1)^2 x(1)*y(1) y(1)^2 x(1) 1;
x(2)^2 x(2)*y(2) y(2)^2 x(2) 1;
x(3)^2 x(3)*y(3) y(3)^2 x(3) 1;
x(4)^2 x(4)*y(4) y(4)^2 x(4) 1;
x(5)^2 x(5)*y(5) y(5)^2 x(5) 1;
x(6)^2 x(6)*y(6) y(6)^2 x(6) 1;
x(7)^2 x(7)*y(7) y(7)^2 x(7) 1;
x(8)^2 x(8)*y(8) y(8)^2 x(8) 1;
x(9)^2 x(9)*y(9) y(9)^2 x(9) 1;
x(10)^2 x(10)*y(10) y(10)^2 x(10) 1 ]
end
The integers 1-10 are meant to be subscripts. I've tried using the syms and repmat functions but have not gotten anywhere further.

 Accepted Answer

x = sym('x', [10 1]);
y = sym('y', [10 1]);
if true
A = [x(1)^2 x(1)*y(1) y(1)^2 x(1) 1;
x(2)^2 x(2)*y(2) y(2)^2 x(2) 1;
x(3)^2 x(3)*y(3) y(3)^2 x(3) 1;
x(4)^2 x(4)*y(4) y(4)^2 x(4) 1;
x(5)^2 x(5)*y(5) y(5)^2 x(5) 1;
x(6)^2 x(6)*y(6) y(6)^2 x(6) 1;
x(7)^2 x(7)*y(7) y(7)^2 x(7) 1;
x(8)^2 x(8)*y(8) y(8)^2 x(8) 1;
x(9)^2 x(9)*y(9) y(9)^2 x(9) 1;
x(10)^2 x(10)*y(10) y(10)^2 x(10) 1 ]
end

11 Comments

Right, this works! This is basically the code that I'm working on. Will it be possible to write matrix A in a single line of code maybe?
if true
%%In this part, we estimate the orbit of the asteroid from the data in
% d.x and d.y
x = d.x;
y = d.y;
% The orbit of an asteroid is given by a conic
% (*) y = c(1)*x.^2 + c(2)*x.*y+c(3)*y.^2+c(4)*x+c(5)
% Find the matrix A and the vector b.
A = sym( x^2 x*y y^2 x 1 ) % FIX THIS!
b = sym('y', [10,1]) % FIX THIS!
% Find the size of the matrix A
size(A)= 10 5
end
Inputing the one that you gave on a new script works great but adding it into this file is bringing up errors. Unexpected MATLAB expression .
Do you have any special interest on working on symbolic? I mean, from your code i deduce you have numeric values for x and y, do you want to use them?
No, I don't actually. While reading up, symbolic seemed like the better choice. There are no numerical values for x and y. Its basically a Ac=b matrix where c = [c1 c2 c3 c4 c5], having to solving for c in terms of x and y.
ok. What about this? :
if true
%%In this part, we estimate the orbit of the asteroid from the data in
% d.x and d.y
x = d.x;
y = d.y;
% The orbit of an asteroid is given by a conic
% (*) y = c(1)*x.^2 + c(2)*x.*y+c(3)*y.^2+c(4)*x+c(5)
% Find the matrix A and the vector b.
x = sym('x', [10 1]);
y = sym('y', [10 1]);
A =[x.^2 x.*y y.^2 x 1] % FIX THIS!
b = sym('y', [10,1]) % FIX THIS!
% Find the size of the matrix A
[r,c]=size(A)
end
I understand what you've written. I created a new script with the code you showed and it gave me the following errors.
Just out of curiosity, is there a way of using repmat here instead? I managed to get it to work with numbers but not with the respective variables.
A =[x.^2 x.*y y.^2 x ones(10,1)] % FIX THIS!
sorry about that.
repmat is used to replicate the same array several times. In your case it's not the same array, it's a different value of the array.
This is great! Just tried it out and it ran perfectly! Right! Different value of array. That genuinely makes more sense.
Thank u for your time and effort! Appreciate it!
you're welcome :)
Hi, I tried this again on another computer and got the following error Undefined function 'sym' for input arguments of type 'char'. Is there suppose to be any additional materials installed in it in order for the sym function to work? When i type help syms its says sum not found. How do i go about this?
Marta Salas
Marta Salas on 14 Mar 2014
Edited: Marta Salas on 14 Mar 2014
Type "ver" on the command line. MATLAB lists all the installed toolboxes. Look for Symbolic Math Toolbox. If you don't have the toolbox, you can not use the symbolic functions.
Right. It does not have the symbolic math toolbox installed. Is there another way to to the same thing?

Sign in to comment.

More Answers (1)

out=[x.^2 x.*y y.^2.*x ones(10,1)]
% x and y are column vextors

Community Treasure Hunt

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

Start Hunting!