如何在Matlab中创建矩阵,每个条目都是双变量函数的输出 [英] How to create a matrix in Matlab with every entry being the output of a bivariate function

查看:560
本文介绍了如何在Matlab中创建矩阵,每个条目都是双变量函数的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个4 x 4矩阵,每个条目代表f(x,y),其中x和y都取值0、1、2和3.所以第一个条目将是f(0,0),一直到f(3,3).

I want to create a 4 x 4 matrix with each entry representing f(x,y) where both x and y take values 0, 1, 2 and 3. So the first entry would be f(0,0), all the way to f(3,3).

函数f(x,y)是:

3 * cos(0 * x + 0 * y)+ 2 * cos(0 * x + 1 * y)+ 3 * cos(0 * x + 2 * y)+ 8 * cos(0 * x + 3 * y) + 3 * cos(1 * x + 0 * y)+ 25 * cos(1 * x + 1 * y)+ 3 * cos(1 * x + 2 * y) + 8 * cos(1 * x + 3 * y) + 3 * cos(2 * x + 0 * y)+ 25 * cos(2 * x + 1 * y)+ 3 * cos(2 * x + 2 * y) + 8 * cos(2 * x + 3 * y) + 3 * cos(3 * x + 0 * y)+ 25 * cos(3 * x + 1 * y)+ 3 * cos(3 * x + 2 * y) -90 * cos(3 * x + 3 * y)

3 * cos(0*x + 0*y) + 2 * cos(0*x + 1*y) + 3 * cos(0*x + 2*y) + 8 * cos(0*x + 3*y) + 3 * cos(1*x + 0*y) + 25 * cos(1*x + 1*y) + 3 * cos(1*x + 2*y) + 8 * cos(1*x + 3*y) + 3 * cos(2*x + 0*y) + 25 * cos(2*x + 1*y) + 3 * cos(2*x + 2*y) + 8 * cos(2*x + 3*y) + 3 * cos(3*x + 0*y) + 25 * cos(3*x + 1*y) + 3 * cos(3*x + 2*y) - 90 * cos(3*x + 3*y)

我没用过Matlab,已经有一段时间了.我试过将f(x,y)转换为@f(x,y)函数;使用.*运算符;对x和y等进行网格划分.所有这些都没有成功...

I haven't used Matlab much, and it's been a while. I have tried turning f(x,y) into a @f(x,y) function; using the .* operator; meshing x and y, etc. All of it without success...

推荐答案

不确定,您尝试了什么,但是使用了

Not sure, what you've tried exactly, but using meshgrid is the correct idea.

% Function defintion (abbreviated)
f = @(x, y) 3 * cos(0*x + 0*y) + 2 * cos(0*x + 1*y) + 3 * cos(0*x + 2*y)

% Set up x and y values.
x = 0:3
y = 0:3

% Generate grid.
[X, Y] = meshgrid(x, y);

% Rseult matrix.
res = f(X, Y)

生成的输出:

f =
   @(x, y) 3 * cos (0 * x + 0 * y) + 2 * cos (0 * x + 1 * y) + 3 * cos (0 * x + 2 * y)

x =
   0   1   2   3

y =
   0   1   2   3

res =
   8.00000   8.00000   8.00000   8.00000
   2.83216   2.83216   2.83216   2.83216
   0.20678   0.20678   0.20678   0.20678
   3.90053   3.90053   3.90053   3.90053

这篇关于如何在Matlab中创建矩阵,每个条目都是双变量函数的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆