# How can I plot functions stored in a matrix?

17 ビュー (過去 30 日間)
PETER SWOVICK 2020 年 3 月 11 日
コメント済み: PETER SWOVICK 2020 年 3 月 11 日
I am trying to use a matrix to try to store a function in a matrix, find its eigenvalues, and then plot those eigenvalues. Is there any way to conveniently do this? Here is the code I have developed so far:
function [ham] = HW2_p3HamGen(block, initial_n)
s=block*2;
ham=sym(zeros(s));
n=initial_n;
for i=1:s
if(mod(i,2)==1)
ham(i,i)=@(w0_w)(w0_w/2+n);
if((i+3)<=s)
ham(i,i+3)=(1/3);
end
if((i-1)>0)
ham(i,i-1)=(1/3);
end
end
if(mod(i,2)==0)
ham(i,i)=@(w0_w)(-w0_w/2+n);
if((i+1)<=s)
ham(i,i+1)=(1/3);
end
if((i-3)>0)
ham(i,i-3)=(1/3);
end
n=n-1;
end
end
end
%This is the script used to find eigenvalues and to plot the eigenvalues
OneBlockHam=HW2_p3HamGen(1,0);
OneBlockVals=eig(OneBlockHam);
w_w=linspace(0,8,1000);
Value1=OneBlockVals(1); %I want this to be a function that I can plot for all values of w_w
plot(w_w,Value1(w_w)) %I want this to plot the function

サインインしてコメントする。

### 回答 (1 件)

Piyush Lakhani 2020 年 3 月 11 日
Yes you can do it by using the "for" loop.
for i=1:1:n % Here n is nuber of times you want to generate eigen values (or n blocks)
OneBlockHam=HW2_p3HamGen(1,0);
OneBlockVals=eig(OneBlockHam);
Value1(:,n)=OneBlockVals(1); % This will make Row matrix where each column has eigen value for perticular block
end
w_w=linspace(0,8,1000);
plot(w_w,Value1(1,:)) %This will plot 1st row of the eigen value matrix
##### 1 件のコメント表示非表示 なし
PETER SWOVICK 2020 年 3 月 11 日
So I tried implementing this like you suggested. But I still got the following error:
Error using plot
Data must be numeric, datetime, duration or an array convertible to double.
Error in PlotQuasiEnergiesCopied (line 10)
plot(w_w,Value1(1,:)) %This will plot 1st row of the eigen value matrix
Would you have any idea why this error would still occur?

サインインしてコメントする。

R2019b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!