0

I've got some code to numerically solve for eigenvectors:

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 is supposed to be the first i eigenvectors, n is the dimension of the matrices (the number of pieces we discretize x into), xmax is the upper limit of the range on which the fxn is defined.

I'm trying to run this from the command line (as: "solveeig # # #", where the number signs correspond to i, n, and xmax) but no matter what I seem to put in for i, n, and xmax, I get "For colon operator with char operands, first and last operands must be char."

What should I be writing on the command line to get this to run?

  • 2
    Kind of "not what you ask for", but what is the issue with solveeig(#,#,#)? Also, do you mean the command window in matlab or a linux terminal or a dos prompt? – patrik Mar 25 '15 at 11:11

1 Answers1

0

Using the command syntax interprets the arguments as strings

For fuller details see the documentation but in short:
Calling

myFun myVar1 6 myVar2

is equivalent to calling

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

and not the desired1

myFun(myVar1,6,myVar2)

In the first cases the function will receive 3 strings (text)
In the second the function will receive the data stored in myVar1 myVar2 and the number 6


The specific error you received is caused by line 2 for j=1:i here i is a string. This error is a merely consequence of the way the function has been called, the line itself is fine2.


How to get it to work

Use function syntax: in the command window something like:

solveeig(n,xmax,i)

If command syntax is absolutely required ( and I can't think why it would be) the much less favourable alternative would be to parse the strings inputted in command syntax. To convert the numbers into numeric formats and use evalin/assignin on the passed variable names to pull variables in from the caller


1As mentioned in comments by patrik
2meaning it won't error, however i and j as variable names is another matter

Community
  • 1
  • 1
RTL
  • 3,577
  • 15
  • 24