2
$\begingroup$

I have a vector-valued function, that is a result of some computation and I would like to plot streamlines of its gradient. I have to multiply the gradient with some vector first, because the gradient is matrix. But when I try to plot the result, all I get is an empty plot.

Here is a simplified code:

data1 = Flatten[Table[{{x, y}, RandomInteger[10]}, {x, 4}, {y, 4}], 1];
data2 = Flatten[Table[{{x, y}, RandomInteger[15]}, {x, 4}, {y, 4}], 1];

f1 = Interpolation[data1];
f2 = Interpolation[data2];
f = {{f1}, {f2}};

g = Grad[f[x, y], {x, y}];
h = g.{{1/2}, {1/2}};

StreamPlot[h[x, y], {x, 1, 4}, {y, 1, 4}]
$\endgroup$

2 Answers 2

3
$\begingroup$

Modify your definitions of f,g,h:

data1 = Flatten[Table[{{x, y}, RandomInteger[10]}, {x, 4}, {y,4}], 1];
data2 = Flatten[Table[{{x, y}, RandomInteger[15]}, {x, 4}, {y,4}],1];

f1 = Interpolation[data1];
f2 = Interpolation[data2];
f = {f1 , f2 }

g = Map[Grad[#[x, y], {x, y}] &, f]
h = Function[{x, y}, g . {{1/2}, {1/2}} // Flatten // Evaluate] 

StreamPlot[h[x, y]   , {x, 1, 4}, {y, 1, 4}]

enter image description here

Hope it helps!

$\endgroup$
1
  • $\begingroup$ It works, thank you. $\endgroup$
    – lemurman
    Oct 5 at 16:02
3
$\begingroup$

Using Through.

Clear["Global`*"];
SeedRandom[1];
data1 = Flatten[Table[{{x, y}, RandomInteger[10]}, {x, 4}, {y, 4}], 1];
data2 = Flatten[Table[{{x, y}, RandomInteger[15]}, {x, 4}, {y, 4}], 1];
f1 = Interpolation[data1];
f2 = Interpolation[data2];

f = {f1, f2};
g = Grad[Through@f[x, y], {x, y}];
StreamPlot[g . {{1/2}, {1/2}}, {x, 1, 4}, {y, 1, 4}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.