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:
Dimension mismatch help

Subject: Dimension mismatch help

From: Karen McKinnon

Date: 27 Apr, 2009 02:34:02

Message: 1 of 4

I am creating a 1D global energy balance model, and am having issues with getting subscript dimension mismatch errors. The code is pasted below. Any ideas? I get the first error when calculating T_o, but I presume I would get the same one for T_a. The size of all matrices is the same (360x3)

nphi = 360;
phi = linspace(0,pi,nphi);
dphi = phi(2) - phi(1);
nt = 182;
totaltime = 365;
t = linspace(0,totaltime,nt);
dt = t(2) - t(1);

rho_a = 1.25; % kg*m^-3
rho_o = 1e3; % kg*m^-3
cp_a = 1004; % J*K^-1*kg^-1
cp_o = 4218; % J*K^-1*kg^-1
epsilon = 0.7; % unitless
sigma = 5.67e-8; % J*s^-1*m^-2*K^-4
alpha = 0.3; %unitless
kappa_o = 0.58; % J*m^-1*s^-1*K^-1
kappa_a = 2.4e-2; % J*m^-1*s^-1*K^-1
delz_a = 5e3; % m
delz_o = 100; % m
R = 6.3781e6; % m



T_a = zeros(nphi,3);
T_o = zeros(nphi,3);
T_o_phi = T_o;
T_o_phiphi = T_o;
T_a_phi = T_o;
T_a_phiphi = T_o;
S = T_o;

temp_atm = zeros(nt,nphi);
temp_oc = temp_atm;





for i = 1:nphi
    for n = 1:3
        
        T_o(i,n) = 0;
        T_a(i,n) = 0;
        
    end
end



for n = 1:nt
    
    
    for i = 2:nphi - 1
        
        phi(i) = i*dphi;
        t(n) = n*dt;
        T_o_phi(i,2) = (T_o(i+1,2) - T_o(i-1,2))/(2*dphi);
        T_o_phiphi(i,2) = (T_o(i+1,1) - 2*T_o(i,1) + T_o(i-1,1))/(dphi*dphi);
        S(i,2) = 125 + 125*sin(1.6*pi*i/nphi + 2.75 + 2.75*sin(2*pi*n/nt + 3*pi/2));
        C1 = ones(3,1)*(2*dt*kappa_o./(R^2.*sin(phi).*cp_o*rho_o*delz_o));
        C2 = ones(3,1)*sin(phi);
        C3 = ones(3,1)*cos(phi);
        T_o(i,3) = T_o(i,1) + C1'.*(C2'.*T_o_phiphi(i,2) + C3'.*T_o_phi(i,2) + (1 - alpha).*S(i,2) - sigma.*T_o(i,2).^4 + epsilon.*sigma.*T_a(i,2).^4);

        
        T_a_phi(i,2) = (T_a(i+1,2) - T_a(i-1,2))/(2*dphi);
        T_a_phiphi(i,2) = (T_a(i+1,1) - 2*T_a(i,1) + T_a(i-1,1))/(dphi*dphi);
        C4 = ones(3,1)*(2*dt*kappa_a./(R^2*sin(phi)*cp_a*rho_a*delz_a));
        T_a(i,3) = T_a(i,1) + C4'*(C2'*T_a_phiphi + C3'*T_a_phi - 2*epsilon*sigma*T_a(i,2).^4 + epsilon*sigma*T_o(i,2).^4);

        
    end

    T_a(nphi,n) = T_a(nphi-1,n);
    T_a(2,n) = T_a(1,n);

    T_a(:,1) = T_a(:,2);
    T_a(:,2) = T_a(:,3);
    
    T_o(nphi,n) = T_o(nphi-1,n);
    T_o(2,n) = T_o(1,n);

    T_o(:,1) = T_o(:,2);
    T_o(:,2) = T_o(:,3);
    
    temp_atm(n,:) = T_a(:,3)';
    temp_oc(n,:) = T_o(:,3)';
    
end

Subject: Dimension mismatch help

From: Gustavo Morales

Date: 27 Apr, 2009 03:11:02

Message: 2 of 4

Karen McKinnon:

Hi Karen!
 I have a question. Why do you do this?:

T_a = zeros(nphi,3);
T_o = zeros(nphi,3);

And then this?:

for i = 1:nphi
    for n = 1:3
        T_o(i,n) = 0;
        T_a(i,n) = 0;
    end
end

I'm afraid are repeated actions...
Make sure you have resumed the code showing relevant info, so we can help you!
:)

Subject: Dimension mismatch help

From: Karen McKinnon

Date: 27 Apr, 2009 12:21:01

Message: 3 of 4

Sorry for the confusion - the loop
> for i = 1:nphi
> for n = 1:3
> T_o(i,n) = 0;
> T_a(i,n) = 0;
> end
> end
is to define initial conditions which I have left at zero for now but will be changed in the future. I'm using the Forward Euler method to try to solve a system of two PDEs.


"Gustavo Morales" <gustavo.morales.2000@gmail.com> wrote in message <gt37o6$1p8$1@fred.mathworks.com>...
> Karen McKinnon:
>
> Hi Karen!
> I have a question. Why do you do this?:
>
> T_a = zeros(nphi,3);
> T_o = zeros(nphi,3);
>
> And then this?:
>
> for i = 1:nphi
> for n = 1:3
> T_o(i,n) = 0;
> T_a(i,n) = 0;
> end
> end
>
> I'm afraid are repeated actions...
> Make sure you have resumed the code showing relevant info, so we can help you!
> :)

Subject: Dimension mismatch help

From: Gustavo Morales

Date: 28 Apr, 2009 03:24:01

Message: 4 of 4

Hi again Karen! (excuse me for my english)

I'm not sure, but you had already defined phi using linspace (at the begining of your code), and then, inside a loop, you redefined with other values. Be carefull of that actions: Difficult the readibility of your code and may produce unexpected results. Now, let's continue...

phi(i) = i*dphi; %Remember phi is a vector of size [1 360] (or [1 nphi])
       % When you created it, phi was: [0 1*dphi 2*dphi 3*dphi ...]
       % Now you're redefining it as: [0 2*dphi 3*dphi 4*dphi ...]
       % ?Do you want to do that?
  
C1 = ones(3,1)*(2*dt*kappa_o./(R^2.*sin(phi).*cp_o*rho_o*delz_o));
C2 = ones(3,1)*sin(phi);
C3 = ones(3,1)*cos(phi);

if size(phi) = [1 nphi] => size(sin(phi)) = [1 nphi] too! So the matrix product
you make: ones(3,1)*(.....sin(phi)) is not compatible.
       
        T_o(i,3) = T_o(i,1) + C1'.*(C2'.*T_o_phiphi(i,2) + C3'.*T_o_phi(i,2) + (1 - alpha).*S(i,2) - sigma.*T_o(i,2).^4 + epsilon.*sigma.*T_a(i,2).^4);

% Look at this: T_o(i,3) is just an single element (size [1 1]) of T_o,
so at the right member of this equation can't be matrices like C1'.*(C2'.*T_o_phiphi(i,2) .... of any other size

I'd like to give you the corrected code, but I don't understand it the equations. See the equations (in a gif file, for example) could help.

Regards from Venezuela!

Tags for 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