0

I am trying to graph a surface with a diagonal matrix, the equation I am trying graph is f = x^TDx, x is a 2 by 1 vector and D is a 2 by 2 matrix.

Here is what have so far, but I keep getting error.

x = linspace(-10,10);
y = linspace(-10,10);
[X,Y] = meshgrid(x,y);
D = [1 0; 0 1];

f = @(x,y) [x,y]*D*[x,y].'; % [x,y] is 1 by 2 

contour (X,Y,f(X,Y))

Can someone tell me how to get rid of the error? Thanks

4
  • You state that "[x,y] is 1 by 2". This is not true, and is the root cause of your problem. Commented Apr 2, 2016 at 6:54
  • @mikkola I don't know why that is not true. I check [x,y] gives a 1 by 2 matrix Commented Apr 2, 2016 at 6:58
  • X and Y are both 100 by 100. So, when you input them to f and there do effectively [X, Y], the result is a 100 by 200 matrix. Commented Apr 2, 2016 at 6:59
  • @mikkola how to make [x,y] be a 2 by 1? I still don't know how Commented Apr 2, 2016 at 7:04

1 Answer 1

2

Since x and y have the same length, your diagonal matrix D must be a square matrix of size n x n, with n equal to two times the length of your x or y vectors. The reason why you need to multiply the length by two is because the operation [x,y] concatenates the arrays horizontally thus duplicating one of the dimensions.

In this example D is the Identity matrix. See eye for more information.

x = linspace(-10,10);   % x is 1x100
y = linspace(-10,10);   % y is 1x100
[X,Y] = meshgrid(x,y);  % X is 100x100 and Y is 100x100
D = eye(2*numel(x));    % D is 2*100x2*100 = 200x200

f = @(x,y) [x,y]*D*[x,y].'; % [X,Y] is 100x200 and [X,Y].' is 200x100

contour (X,Y,f(X,Y))

If you want D to be a random diagonal matrix, you can accomplish this combining diag with one of the Random Number Generation functions available, like for example randn.

On the previous example, replace D with the following instruction:

D = diag(randn(1,2*numel(x)));

You can also give the coefficients you choose to the diagonal matrix. To do so, you will need to create the vector of coefficients manually, making sure that it has the adequate length, so that it satisfies the conditions explained at the beginning of this post.

Try now replacing D with the following instructions:

v = 1:2*numel(x);   % vector of coefficients: v = [1 2 ... 200]
D = diag(v);
Sign up to request clarification or add additional context in comments.

2 Comments

If D isn't a identity matrix, it is a random diagonal matrix, how could I do it
@Simple I have edited the answer to adress your request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.