How can I plot functions stored in a matrix?

5 views (last 30 days)
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

Answers (1)

Piyush Lakhani
Piyush Lakhani on 11 Mar 2020
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 Comment
PETER SWOVICK
PETER SWOVICK on 11 Mar 2020
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?

Sign in to comment.

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!