Matlab:特征函数求解器的命令行输入(数值方法)

Matlab: Command Line Inputs for an Eigenfunction Solver (Numerical Method)

我有一些代码可以对特征向量进行数值求解:

function[efun,V,D] = solveeig(n,xmax,i)
for j=1:i

%The first and second derivative matrices
dd = 1/(xmax/n)^2*(-2*diag(ones(n,1))+diag(ones(n-1,1),1)+...
diag(ones(n-1,1),-1));
d = 1/(xmax/n)*((-1*diag(ones(n,1)))+diag(ones(n-1,1),1));

%solve for the eigenvectors
[V,D] = eig(-dd-2*d);

%plot the eigenvectors (normalized) with the normalized calculated
%eigenfunctions
x = linspace(0,xmax,n);
subplot(i,1,j);
plot(x,V(:,j)/sum(V(:,j)),'*');
hold on
efun = exp(-x).*sin(j*pi*x/xmax);
plot(x,efun/(sum(efun)),'r');
shg
end
end

i应该是第i个特征向量,n是第i个的维数 矩阵(我们将x离散化成的块数),xmax是定义fxn的范围的上限。

我正在尝试从命令行 运行 这个(如:"solveeig # # #",其中数字符号对应于 i、n 和 xmax),但无论我看起来是什么对于 i、n 和 xmax,我得到 "For colon operator with char operands, first and last operands must be char."

我应该在命令行上写什么才能将其发送到 运行?

使用命令语法将参数解释为字符串

有关更完整的详细信息,请参阅 the documentation 但简而言之:
打电话

myFun myVar1 6 myVar2

相当于调用

myFun('myVar1','6','myVar2')

而不是想要的1

myFun(myVar1,6,myVar2)

在第一种情况下,该函数将接收 3 个字符串(文本)
第二个函数将接收存储在 myVar1 myVar2 中的数据和数字 6


您收到的特定错误是由第 2 行 for j=1:i 引起的,这里 i 是一个字符串。此错误仅仅是函数调用方式的结果,该行本身很好2.


如何让它工作

使用函数语法:在命令中 window 类似于:

solveeig(n,xmax,i)

如果绝对需要命令语法(而且我想不出为什么会这样),更不受欢迎 替代方案是解析在命令语法中输入的字符串。将数字转换为数字格式并在传递的变量名称上使用 evalin/assignin 以从调用者

中提取变量

1如 patrik
评论中所述 2意味着它不会出错,但是 ij 作为变量名是 another matter