Simple
Simple

Reputation: 135

Contour plot with matrices in matlab

I have a function f = x^2-2xy+y^2, this is the result of 3 2 by 2 matrices after I calculated that by hand. I know how to contour plot with a given function like f. If I have multiple n by m matrices, how do plot them in matlab without calculating them to get f.

Such as, A = [1 0;-1 1; 0 -1]; D = [1 0 0;0 2 0; 0 0 4]; then I will have f = [x y]^TA^TDA[x y]. The only way I know is f=@(x,y) [x,y].'A^TDA[x,y], but this gives me an error since the dimensions don't agree.

Is there a way to do this? Thanks

Upvotes: 0

Views: 172

Answers (1)

user1543042
user1543042

Reputation: 3440

Based on your description, which you need to do a better job describing.

[X, Y] = meshgrid(0:0.1:10, 0:0.1:10);
f =@(x,y) cellfun(@(c) c*A'*D*A*c', num2cell([x,y],2));
colormap(jet);
contourf(X, Y, reshape(f(X(:), Y(:)), size(X)),20,'LineStyle','none')

Upvotes: 1

Related Questions