How can I plot functions stored in a matrix?

조회 수: 5 (최근 30일)
PETER SWOVICK
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
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
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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by