Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
using imag function in matlab

Subject: using imag function in matlab

From: Teddy

Date: 4 Apr, 2009 06:50:36

Message: 1 of 2

hi i am trying to make this work but I just dont get where I am going wrong.

very last bit of osamt.m is where I am having problem. I get an error which is

??? Subscript indices must either be real positive integers or logicals.

Error in ==> osmat at 65
   x1=imag(Tn*xs(:,o))

Tn*xs(:,o) works alright but I think it is to do with extracting imaginary part of Tn*xs(:,o).






osmat.m
------------------------------------------------------
clear all
clc
format long


zi=sqrt(-1);


%Generate Chebyshev differentiation matrices
nosmod=50;
alp=1.02;
beta=0;
R=5772;

[D0, D1, D2, D4]=Dmat(nosmod);



%compute the Orr-Sommerfeld matrix (by inverting B)
d=inv(B)*A;

%ordered eigenvalues
[v,e]=eig(d);
e=diag(e);
[imag,is]=sort(-imag(e));
xs=v(:,is);
es=e(is);



%Calculate eigenfunctions using Chebyshev approximation
y=-1:0.01:1;
for n=1:102
    Tn(:,n)=cos((n-1).*(cos(1.*y)));
end



%Plotting eigenfunctions calculated
for o=1:1:50
   x1=imag(Tn*xs(:,o))
   x2=real(Tn*xs(:,o))
   plot(y,x1,'+',y,x2,'-')
   grid;
end
------------------------------------------------------







Help would be great. I will attach other 2 matlab files








Dmat.m
------------------------------------------------------
function[D0,D1,D2,D4]=Dmat(N);

%
%function to create differentiation matrices
%
%N= number of modes
%D0=zero'th derivative matrix
%D1=first derivative matrix
%D2=second derivative matrix
%D4=fourth derivative matrix

%initialize

num=round(abs(N))

%create D0

D0=[];
vec=(0:1:num)';
for j=0:1:num
    D0=[D0 cos(j*pi*vec/num)];
end;

%create higher derivative matrices

lv=length(vec);
D1=[zeros(lv,1) D0(:,1) 4*D0(:,2)];
D2=[zeros(lv,1) zeros(lv,1) 4*D0(:,1)];
D3=[zeros(lv,1) zeros(lv,1) zeros(lv,1)];
D4=[zeros(lv,1) zeros(lv,1) zeros(lv,1)];
for j=3:num
    D1=[D1 2*j*D0(:,j)+j*D1(:,j-1)/(j-2)];
    D2=[D2 2*j*D1(:,j)+j*D2(:,j-1)/(j-2)];
    D3=[D3 2*j*D2(:,j)+j*D3(:,j-1)/(j-2)];
    D4=[D4 2*j*D3(:,j)+j*D4(:,j-1)/(j-2)];
end;
------------------------------------------------------











pois.m
------------------------------------------------------
function [A,B]=pois(nosmod,alp,beta,R,D0,D1,D2,D4);

%function to creat Orr-Sommerfeld matrices using chebyshev
%pseudospectral discretization for plane poiseuille flow
%profile
%
%nosmod = number of modes
%alp= alpha
%beta= beta
%R= Reynolds number
%D0 = zero'th derivative matrix
%D1 = first
%D2= second
%D3=third
%D4 = fourth

zi=sqrt(-1);

%mean velocity

ak2=alp^2+beta^2;
Nos=nosmod+1;
Nsq=nosmod+1;

vec=(0:1:nosmod)';
u=(ones(length(vec),1)-cos(pi*vec/nosmod).^2);
du=-2*cos(pi*vec/nosmod);

%set up Orr-Sommerfeld matrix

B11=D2-ak2*D0;
A11=-(D4-2*ak2*D2+(ak2^2)*D0)/(zi*R);
A11=A11+alp*(u*ones(1,length(u))).*B11+alp*2*D0;
er=-200*zi;
A11=[er*D0(1,:); er*D1(1,:); A11(3:Nos-2,:); ...
    er*D1(Nos,:); er*D0(Nos,:)];
B11=[D0(1,:); D1(1,:); B11(3:Nos-2,:); ...
    D1(Nos,:); D0(Nos,:)];

%set up Squire matrix and cross term matrix

A21=beta*(du*ones(1,length(u))).*D0(1:Nos,:);
A22=alp*(u*ones(1,length(u))).*D0-(D2-ak2*D0)/(zi*R);
B22=D0;
A22=[er*D0(1,:); A22(2:Nsq-1,:); er*D0(Nsq,:)];
A21=[zeros(1,Nos); A21(2:Nsq-1,:); zeros(1,Nos)];

%combine all the blocks

A=[A11 zeros(Nos,Nsq); A21 A22];
B=[B11 zeros(Nos,Nsq); zeros(Nsq,Nos) B22];
------------------------------------------------------

Subject: using imag function in matlab

From: Roger Stafford

Date: 4 Apr, 2009 07:34:01

Message: 2 of 2

Teddy <skim178@ec.auckland.ac.nz> wrote in message <14052661.25447.1238827868892.JavaMail.jakarta@nitrogen.mathforum.org>...
> .......
> [imag,is]=sort(-imag(e));
> .......

  In osmat.m at the line:

 [imag,is]=sort(-imag(e));

you have usurped the name "imag" for the output array coming from 'sort', so it can no longer function in its intended way of finding the imaginary parts of complex numbers. You should never use matlab's dedicated function names for other purposes like this. It is undoubtedly the source of your trouble.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us