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:
Coding error

Subject: Coding error

From: SF88

Date: 28 Sep, 2010 13:23:21

Message: 1 of 8

Hi everyone, when I run the 1st Code and then type X is gives me 401 values for which X starts at 20 and rises to 90 and then stays at 90 from then on which is exactly what I'm after. When I run the 2nd Code however, instead of giving me for example that T(79) is then equal to X it gives me an error. Can anyone help? I think it's because T may already be a vector and then when I'm telling it T(79) is equal to something it's saying it can't be is that correct? If so is there a way I can get around this because I actually have T values from 1 to 91, 1 to 78 of which are formulas which work fine, but 79 to 91 have to change with time.

1st Code:

t = 0:dt:20;
for m=1:numel(t)
    if t(m)==0
        X(m)=20;
    elseif t(m)>0 && t(m)<2
        X(m)=20+35*t(m);
    elseif t(m)>=2
        X(m)=90;
    end
end

2nd Code:

T(79)=X;
T(80)=X;
T(81)=X;
T(82)=X;
T(83)=X;
T(84)=X;
T(85)=X;
T(86)=X;
T(87)=X;
T(88)=X;
T(89)=X;
T(90)=X;
T(91)=X;

Subject: Coding error

From: Richard Crozier

Date: 28 Sep, 2010 14:02:21

Message: 2 of 8

"SF88 " <blah@hotmail.com> wrote in message <i7sq89$gup$1@fred.mathworks.com>...
> Hi everyone, when I run the 1st Code and then type X is gives me 401 values for which X starts at 20 and rises to 90 and then stays at 90 from then on which is exactly what I'm after. When I run the 2nd Code however, instead of giving me for example that T(79) is then equal to X it gives me an error. Can anyone help? I think it's because T may already be a vector and then when I'm telling it T(79) is equal to something it's saying it can't be is that correct? If so is there a way I can get around this because I actually have T values from 1 to 91, 1 to 78 of which are formulas which work fine, but 79 to 91 have to change with time.
>
> 1st Code:
>
> t = 0:dt:20;
> for m=1:numel(t)
> if t(m)==0
> X(m)=20;
> elseif t(m)>0 && t(m)<2
> X(m)=20+35*t(m);
> elseif t(m)>=2
> X(m)=90;
> end
> end
>
> 2nd Code:
>
> T(79)=X;
> T(80)=X;
> T(81)=X;
> T(82)=X;
> T(83)=X;
> T(84)=X;
> T(85)=X;
> T(86)=X;
> T(87)=X;
> T(88)=X;
> T(89)=X;
> T(90)=X;
> T(91)=X;

In the example you gave, the variable dt is not set, which will effect the exact reason for the error. Tell us what dt is, and also copy and paste the output from the matlab command line when you run the code to show the exact error you get.

Subject: Coding error

From: SF88

Date: 28 Sep, 2010 14:22:03

Message: 3 of 8

Sorry Richard,

There is indeed a heap more coding above the cut-outs that I pasted, and a part of it in fact was dt=0.05

I'll post some more to be safe:

k = 230; % Thermal conductivity (W/m^2 degrees celsius)
        rho = 2700; % Density of ceramic clay (kg/m^3)
        c = 910; % Specific heat coefficient (J/kg degrees celsius)
        h = 300; % Convective heat transfer coefficient (W/m^2 degrees celsius)
        Tinf = 20; % Temperature on the exposed surface (degrees celsius)
        dx = 0.005; % Step size in x-direction (m)
        dy = 0.005; % Step size in y-direction (m)
        DVcorners = (dx*dy)/4; % Volume of corner nodes
        DVedge = (dx*dy)/2; % Volume of edge nodes
        DVinternal = (dx*dy); % Volume of internal nodes
        Zcorners = (rho*c*DVcorners); % Creating constants to save some repetition
        Zedge = (rho*c*DVedge);
        Zinternal = (rho*c*DVinternal);
        Ax = dy/2; % Area at node
        Ay = dx/2;
        Ti = 20*ones(1,91)'; % Initial temperature at nodes
        T0 = Ti;
        dt = 0.05; % Time step (s)
        
        % Creates the boundary condition for the bottom edge (nodes 79-91)
        t = 0:dt:20;
        for m=1:numel(t)
            if t(m)==0
                X(m)=20;
            elseif t(m)>0 && t(m)<2
                X(m)=20+35*t(m);
            elseif t(m)>=2
                X(m)=90;
            end
        end
        
        % Calculates the temperature at each node over the specified time
        ivals = 0:dt:20;
        for i = 1:length(ivals)
                T = Ti;

for j=1:numel(t)
                    T(79)=X(j);
                    T(80)=X(j);
                    T(81)=X(j);
                    T(82)=X(j);
                    T(83)=X(j);
                    T(84)=X(j);
                    T(85)=X(j);
                    T(86)=X(j);
                    T(87)=X(j);
                    T(88)=X(j);
                    T(89)=X(j);
                    T(90)=X(j);
                    T(91)=X(j);
                end
                Ti = T;
                Tfinal(:,1) = T0;
                Tfinal(:,i) = Ti;

It goes something like that. Again there is a little more info in my script that I haven't ncluded because I think is not required. When I run it, it runs fine, but it's saying that the values for T(79) all the way to T(91) are simply equal to 90. When really I want them to be a vector 401 time steps long so that for time step 1-401 T(79) will change from 20 to 90 degrees. Does that make sense? If not I'll simply paste my full code because there may be something in there.

Cheers

Subject: Coding error

From: Richard Crozier

Date: 28 Sep, 2010 15:06:19

Message: 4 of 8

"SF88 " <blah@hotmail.com> wrote in message <i7stmb$cuu$1@fred.mathworks.com>...
> Sorry Richard,
>
> There is indeed a heap more coding above the cut-outs that I pasted, and a part of it in fact was dt=0.05
>
> I'll post some more to be safe:
>
> k = 230; % Thermal conductivity (W/m^2 degrees celsius)
> rho = 2700; % Density of ceramic clay (kg/m^3)
> c = 910; % Specific heat coefficient (J/kg degrees celsius)
> h = 300; % Convective heat transfer coefficient (W/m^2 degrees celsius)
> Tinf = 20; % Temperature on the exposed surface (degrees celsius)
> dx = 0.005; % Step size in x-direction (m)
> dy = 0.005; % Step size in y-direction (m)
> DVcorners = (dx*dy)/4; % Volume of corner nodes
> DVedge = (dx*dy)/2; % Volume of edge nodes
> DVinternal = (dx*dy); % Volume of internal nodes
> Zcorners = (rho*c*DVcorners); % Creating constants to save some repetition
> Zedge = (rho*c*DVedge);
> Zinternal = (rho*c*DVinternal);
> Ax = dy/2; % Area at node
> Ay = dx/2;
> Ti = 20*ones(1,91)'; % Initial temperature at nodes
> T0 = Ti;
> dt = 0.05; % Time step (s)
>
> % Creates the boundary condition for the bottom edge (nodes 79-91)
> t = 0:dt:20;
> for m=1:numel(t)
> if t(m)==0
> X(m)=20;
> elseif t(m)>0 && t(m)<2
> X(m)=20+35*t(m);
> elseif t(m)>=2
> X(m)=90;
> end
> end
>
> % Calculates the temperature at each node over the specified time
> ivals = 0:dt:20;
> for i = 1:length(ivals)
> T = Ti;
>
> for j=1:numel(t)
> T(79)=X(j);
> T(80)=X(j);
> T(81)=X(j);
> T(82)=X(j);
> T(83)=X(j);
> T(84)=X(j);
> T(85)=X(j);
> T(86)=X(j);
> T(87)=X(j);
> T(88)=X(j);
> T(89)=X(j);
> T(90)=X(j);
> T(91)=X(j);
> end
> Ti = T;
> Tfinal(:,1) = T0;
> Tfinal(:,i) = Ti;
>
> It goes something like that. Again there is a little more info in my script that I haven't ncluded because I think is not required. When I run it, it runs fine, but it's saying that the values for T(79) all the way to T(91) are simply equal to 90. When really I want them to be a vector 401 time steps long so that for time step 1-401 T(79) will change from 20 to 90 degrees. Does that make sense? If not I'll simply paste my full code because there may be something in there.
>
> Cheers

First of all, I think you are missing an 'end' somewhere in the code posted.

Next, well, I may have misunderstood what you're asking, but T(79) can never be a vector. It is a single value in a vector. T(79, :) could be a row vector, or T(:,79) could be a column vector, but not T(79), this can be only one value. For example

Declare a 3x3 matrix

M = [1 2 3;
       4 5 6;
       7 8 9]
M =
     1 2 3
     4 5 6
     7 8 9

M(1)
ans =
     1

M(1,:)
ans =
     1 2 3

M(:,1)
ans =
     1
     4
     7

Declare a column vector similar to yours:

T = [1; 2; 3; 4; 5]
T =
     1
     2
     3
     4
     5

T(1)
ans =
     1
T(4)
ans =
     4

In your code you have a loop like this:

    for j=1:numel(t)
        T(79)=X(j);
        T(80)=X(j);
        T(81)=X(j);
        T(82)=X(j);
        T(83)=X(j);
        T(84)=X(j);
        T(85)=X(j);
        T(86)=X(j);
        T(87)=X(j);
        T(88)=X(j);
        T(89)=X(j);
        T(90)=X(j);
        T(91)=X(j);
    end

All this loop does is replace the single value in each of the points T(79), T(80), T(81) etc with another single value, the single value in the location X(j). This can be shown in the following similar example:

t = 1:5;
X = [10, 9, 8, 7, 6];
T = [0, 0];
for j=1:numel(t)
        T(2)=X(j)
end

The output is:

T =
     0 10
T =
     0 9
T =
     0 8
T =
     0 7
T =
     0 6

I'm guessing this is not want you want.

If you wish to solve differential equations numerically in MATLAB, I would suggest using the built-in ode solvers, look up ode45 for instance.

Subject: Coding error

From: SF88

Date: 28 Sep, 2010 15:22:21

Message: 5 of 8

Richard. What you are saying is making sense. I'll post the full script to save confusions but I'll first explain what I'm trying to achieve. I have a heat plate that i've separated into 91 nodes. The temperature in the first 78 nodes depends on their surrounding nodes as you can see, then the temperature in nodes 79-91 are from a set formula so that they start at 20 degrees and rise to 90 degrees over 2sec then remain at 90 degrees from then on. I've coded in the first 78 nodes correctly I believe, but it's the remaining 13 I'm having trouble with.

k = 230; % Thermal conductivity (W/m^2 degrees celsius)
        rho = 2700; % Density of ceramic clay (kg/m^3)
        c = 910; % Specific heat coefficient (J/kg degrees celsius)
        h = 300; % Convective heat transfer coefficient (W/m^2 degrees celsius)
        Tinf = 20; % Temperature on the exposed surface (degrees celsius)
        dx = 0.005; % Step size in x-direction (m)
        dy = 0.005; % Step size in y-direction (m)
        DVcorners = (dx*dy)/4; % Volume of corner nodes
        DVedge = (dx*dy)/2; % Volume of edge nodes
        DVinternal = (dx*dy); % Volume of internal nodes
        Zcorners = (rho*c*DVcorners); % Creating constants to save some repetition
        Zedge = (rho*c*DVedge);
        Zinternal = (rho*c*DVinternal);
        Ax = dy/2; % Area at node
        Ay = dx/2;
        Ti = 20*ones(1,91)'; % Initial temperature at nodes
        T0 = Ti;
        dt = 0.05; % Time step (s)
        
        % Creates the boundary condition for the bottom edge (nodes 79-91)
        t = 0:dt:20;
        for m=1:numel(t)
            if t(m)==0
                X(m)=20;
            elseif t(m)>0 && t(m)<2
                X(m)=20+35*t(m);
            elseif t(m)>=2
                X(m)=90;
            end
        end
        
        % Calculates the temperature at each node over the specified time
        ivals = 0:dt:20;
        for i = 1:length(ivals)
                T = Ti;
                T(1) = (((k/2)*(Ti(14)-Ti(1)) + (k/2)*(Ti(2)-Ti(1)) + h*Ax*(Tinf-Ti(1)))*(dt/Zcorners)) + Ti(1);
                T(2) = (((k/2)*(Ti(1)-Ti(2)) + k*(Ti(15)-Ti(2)) + (k/2)*(Ti(3)-Ti(2)) + h*Ax*(Tinf-Ti(2)))*(dt/Zedge)) + Ti(2);
                T(3) = (((k/2)*(Ti(2)-Ti(3)) + k*(Ti(16)-Ti(3)) + (k/2)*(Ti(4)-Ti(3)) + h*Ax*(Tinf-Ti(3)))*(dt/Zedge)) + Ti(3);
                T(4) = (((k/2)*(Ti(3)-Ti(4)) + k*(Ti(17)-Ti(4)) + (k/2)*(Ti(5)-Ti(4)) + h*Ax*(Tinf-Ti(4)))*(dt/Zedge)) + Ti(4);
                T(5) = (((k/2)*(Ti(4)-Ti(5)) + k*(Ti(18)-Ti(5)) + (k/2)*(Ti(6)-Ti(5)) + h*Ax*(Tinf-Ti(5)))*(dt/Zedge)) + Ti(5);
                T(6) = (((k/2)*(Ti(5)-Ti(6)) + k*(Ti(19)-Ti(6)) + (k/2)*(Ti(7)-Ti(6)) + h*Ax*(Tinf-Ti(6)))*(dt/Zedge)) + Ti(6);
                T(7) = (((k/2)*(Ti(6)-Ti(7)) + k*(Ti(20)-Ti(7)) + (k/2)*(Ti(8)-Ti(7)) + h*Ax*(Tinf-Ti(7)))*(dt/Zedge)) + Ti(7);
                T(8) = (((k/2)*(Ti(7)-Ti(8)) + k*(Ti(21)-Ti(8)) + (k/2)*(Ti(9)-Ti(8)) + h*Ax*(Tinf-Ti(8)))*(dt/Zedge)) + Ti(8);
                T(9) = (((k/2)*(Ti(8)-Ti(9)) + k*(Ti(22)-Ti(9)) + (k/2)*(Ti(10)-Ti(9)) + h*Ax*(Tinf-Ti(9)))*(dt/Zedge)) + Ti(9);
                T(10) = (((k/2)*(Ti(9)-Ti(10)) + k*(Ti(23)-Ti(10)) + (k/2)*(Ti(11)-Ti(10)) + h*Ax*(Tinf-Ti(10)))*(dt/Zedge)) + Ti(10);
                T(11) = (((k/2)*(Ti(10)-Ti(11)) + k*(Ti(24)-Ti(11)) + (k/2)*(Ti(12)-Ti(11)) + h*Ax*(Tinf-Ti(11)))*(dt/Zedge)) + Ti(11);
                T(12) = (((k/2)*(Ti(11)-Ti(12)) + k*(Ti(25)-Ti(12)) + (k/2)*(Ti(13)-Ti(12)) + h*Ax*(Tinf-Ti(12)))*(dt/Zedge)) + Ti(12);
                T(13) = (((k/2)*(Ti(12)-Ti(13)) + (k/2)*(Ti(26)-Ti(13)) + h*Ay*(Tinf-Ti(13)) + h*Ax*(Tinf-Ti(13)))*(dt/Zcorners)) + Ti(13);
                T(14) = (((k/2)*(Ti(27)-Ti(14)) + k*(Ti(15)-Ti(14)) + (k/2)*(Ti(1)-Ti(14)))*(dt/Zedge)) + Ti(14);
                T(15) = ((k*(Ti(14)-Ti(15)) + k*(Ti(28)-Ti(15)) + k*(Ti(16)-Ti(15)) + k*(Ti(2)-Ti(15)))*(dt/Zinternal)) + Ti(15);
                T(16) = ((k*(Ti(15)-Ti(16)) + k*(Ti(29)-Ti(16)) + k*(Ti(17)-Ti(16)) + k*(Ti(3)-Ti(16)))*(dt/Zinternal)) + Ti(16);
                T(17) = ((k*(Ti(16)-Ti(17)) + k*(Ti(30)-Ti(17)) + k*(Ti(18)-Ti(17)) + k*(Ti(4)-Ti(17)))*(dt/Zinternal)) + Ti(17);
                T(18) = ((k*(Ti(17)-Ti(18)) + k*(Ti(31)-Ti(18)) + k*(Ti(19)-Ti(18)) + k*(Ti(5)-Ti(18)))*(dt/Zinternal)) + Ti(18);
                T(19) = ((k*(Ti(18)-Ti(19)) + k*(Ti(32)-Ti(19)) + k*(Ti(20)-Ti(19)) + k*(Ti(6)-Ti(19)))*(dt/Zinternal)) + Ti(19);
                T(20) = ((k*(Ti(19)-Ti(20)) + k*(Ti(33)-Ti(20)) + k*(Ti(21)-Ti(20)) + k*(Ti(7)-Ti(20)))*(dt/Zinternal)) + Ti(20);
                T(21) = ((k*(Ti(20)-Ti(21)) + k*(Ti(34)-Ti(21)) + k*(Ti(22)-Ti(21)) + k*(Ti(8)-Ti(21)))*(dt/Zinternal)) + Ti(21);
                T(22) = ((k*(Ti(21)-Ti(22)) + k*(Ti(35)-Ti(22)) + k*(Ti(23)-Ti(22)) + k*(Ti(9)-Ti(22)))*(dt/Zinternal)) + Ti(22);
                T(23) = ((k*(Ti(22)-Ti(23)) + k*(Ti(36)-Ti(23)) + k*(Ti(24)-Ti(23)) + k*(Ti(10)-Ti(23)))*(dt/Zinternal)) + Ti(23);
                T(24) = ((k*(Ti(23)-Ti(24)) + k*(Ti(37)-Ti(24)) + k*(Ti(25)-Ti(24)) + k*(Ti(11)-Ti(24)))*(dt/Zinternal)) + Ti(24);
                T(25) = ((k*(Ti(24)-Ti(25)) + k*(Ti(38)-Ti(25)) + k*(Ti(26)-Ti(25)) + k*(Ti(12)-Ti(25)))*(dt/Zinternal)) + Ti(25);
                T(26) = ((k*(Ti(25)-Ti(26)) + (k/2)*(Ti(39)-Ti(26)) + h*Ay*(Tinf-Ti(26)) + (k/2)*(Ti(13)-Ti(26)))*(dt/Zedge)) + Ti(26);
                T(27) = (((k/2)*(Ti(40)-Ti(27)) + k*(Ti(28)-Ti(27)) + (k/2)*(Ti(14)-Ti(27)))*(dt/Zedge)) + Ti(27);
                T(28) = ((k*(Ti(27)-Ti(28)) + k*(Ti(41)-Ti(28)) + k*(Ti(29)-Ti(28)) + k*(Ti(15)-Ti(28)))*(dt/Zinternal)) + Ti(28);
                T(29) = ((k*(Ti(28)-Ti(29)) + k*(Ti(42)-Ti(29)) + k*(Ti(30)-Ti(29)) + k*(Ti(16)-Ti(29)))*(dt/Zinternal)) + Ti(29);
                T(30) = ((k*(Ti(29)-Ti(30)) + k*(Ti(43)-Ti(30)) + k*(Ti(31)-Ti(30)) + k*(Ti(17)-Ti(30)))*(dt/Zinternal)) + Ti(30);
                T(31) = ((k*(Ti(30)-Ti(31)) + k*(Ti(44)-Ti(31)) + k*(Ti(32)-Ti(31)) + k*(Ti(18)-Ti(31)))*(dt/Zinternal)) + Ti(31);
                T(32) = ((k*(Ti(31)-Ti(32)) + k*(Ti(45)-Ti(32)) + k*(Ti(33)-Ti(32)) + k*(Ti(19)-Ti(32)))*(dt/Zinternal)) + Ti(32);
                T(33) = ((k*(Ti(32)-Ti(33)) + k*(Ti(46)-Ti(33)) + k*(Ti(34)-Ti(33)) + k*(Ti(20)-Ti(33)))*(dt/Zinternal)) + Ti(33);
                T(34) = ((k*(Ti(33)-Ti(34)) + k*(Ti(47)-Ti(34)) + k*(Ti(35)-Ti(34)) + k*(Ti(21)-Ti(34)))*(dt/Zinternal)) + Ti(34);
                T(35) = ((k*(Ti(34)-Ti(35)) + k*(Ti(48)-Ti(35)) + k*(Ti(36)-Ti(35)) + k*(Ti(22)-Ti(35)))*(dt/Zinternal)) + Ti(35);
                T(36) = ((k*(Ti(35)-Ti(36)) + k*(Ti(49)-Ti(36)) + k*(Ti(37)-Ti(36)) + k*(Ti(23)-Ti(36)))*(dt/Zinternal)) + Ti(36);
                T(37) = ((k*(Ti(36)-Ti(37)) + k*(Ti(50)-Ti(37)) + k*(Ti(38)-Ti(37)) + k*(Ti(24)-Ti(37)))*(dt/Zinternal)) + Ti(37);
                T(38) = ((k*(Ti(37)-Ti(38)) + k*(Ti(51)-Ti(38)) + k*(Ti(39)-Ti(38)) + k*(Ti(25)-Ti(38)))*(dt/Zinternal)) + Ti(38);
                T(39) = ((k*(Ti(38)-Ti(39)) + (k/2)*(Ti(52)-Ti(39)) + h*Ay*(Tinf-Ti(39)) + (k/2)*(Ti(26)-Ti(39)))*(dt/Zedge)) + Ti(39);
                T(40) = (((k/2)*(Ti(53)-Ti(40)) + k*(Ti(41)-Ti(40)) + (k/2)*(Ti(27)-Ti(40)))*(dt/Zedge)) + Ti(40);
                T(41) = ((k*(Ti(40)-Ti(41)) + k*(Ti(54)-Ti(41)) + k*(Ti(42)-Ti(41)) + k*(Ti(28)-Ti(41)))*(dt/Zinternal)) + Ti(41);
                T(42) = ((k*(Ti(41)-Ti(42)) + k*(Ti(55)-Ti(42)) + k*(Ti(43)-Ti(42)) + k*(Ti(29)-Ti(42)))*(dt/Zinternal)) + Ti(42);
                T(43) = ((k*(Ti(42)-Ti(43)) + k*(Ti(56)-Ti(43)) + k*(Ti(44)-Ti(43)) + k*(Ti(30)-Ti(43)))*(dt/Zinternal)) + Ti(43);
                T(44) = ((k*(Ti(43)-Ti(44)) + k*(Ti(57)-Ti(44)) + k*(Ti(45)-Ti(44)) + k*(Ti(31)-Ti(44)))*(dt/Zinternal)) + Ti(44);
                T(45) = ((k*(Ti(44)-Ti(45)) + k*(Ti(58)-Ti(45)) + k*(Ti(46)-Ti(45)) + k*(Ti(32)-Ti(45)))*(dt/Zinternal)) + Ti(45);
                T(46) = ((k*(Ti(45)-Ti(46)) + k*(Ti(59)-Ti(46)) + k*(Ti(47)-Ti(46)) + k*(Ti(33)-Ti(46)))*(dt/Zinternal)) + Ti(46);
                T(47) = ((k*(Ti(46)-Ti(47)) + k*(Ti(60)-Ti(47)) + k*(Ti(48)-Ti(47)) + k*(Ti(34)-Ti(47)))*(dt/Zinternal)) + Ti(47);
                T(48) = ((k*(Ti(47)-Ti(48)) + k*(Ti(61)-Ti(48)) + k*(Ti(49)-Ti(48)) + k*(Ti(35)-Ti(48)))*(dt/Zinternal)) + Ti(48);
                T(49) = ((k*(Ti(48)-Ti(49)) + k*(Ti(62)-Ti(49)) + k*(Ti(50)-Ti(49)) + k*(Ti(36)-Ti(49)))*(dt/Zinternal)) + Ti(49);
                T(50) = ((k*(Ti(49)-Ti(50)) + k*(Ti(63)-Ti(50)) + k*(Ti(51)-Ti(50)) + k*(Ti(37)-Ti(50)))*(dt/Zinternal)) + Ti(50);
                T(51) = ((k*(Ti(50)-Ti(51)) + k*(Ti(64)-Ti(51)) + k*(Ti(52)-Ti(51)) + k*(Ti(38)-Ti(51)))*(dt/Zinternal)) + Ti(51);
                T(52) = ((k*(Ti(51)-Ti(52)) + (k/2)*(Ti(65)-Ti(52)) + h*Ay*(Tinf-Ti(52)) + (k/2)*(Ti(39)-Ti(52)))*(dt/Zedge)) + Ti(52);
                T(53) = (((k/2)*(Ti(66)-Ti(53)) + k*(Ti(54)-Ti(53)) + (k/2)*(Ti(40)-Ti(53)))*(dt/Zedge)) + Ti(53);
                T(54) = ((k*(Ti(53)-Ti(54)) + k*(Ti(67)-Ti(54)) + k*(Ti(55)-Ti(54)) + k*(Ti(41)-Ti(54)))*(dt/Zinternal)) + Ti(54);
                T(55) = ((k*(Ti(54)-Ti(55)) + k*(Ti(68)-Ti(55)) + k*(Ti(56)-Ti(55)) + k*(Ti(42)-Ti(55)))*(dt/Zinternal)) + Ti(55);
                T(56) = ((k*(Ti(55)-Ti(56)) + k*(Ti(69)-Ti(56)) + k*(Ti(57)-Ti(56)) + k*(Ti(43)-Ti(56)))*(dt/Zinternal)) + Ti(56);
                T(57) = ((k*(Ti(56)-Ti(57)) + k*(Ti(70)-Ti(57)) + k*(Ti(58)-Ti(57)) + k*(Ti(44)-Ti(57)))*(dt/Zinternal)) + Ti(57);
                T(58) = ((k*(Ti(57)-Ti(58)) + k*(Ti(71)-Ti(58)) + k*(Ti(59)-Ti(58)) + k*(Ti(45)-Ti(58)))*(dt/Zinternal)) + Ti(58);
                T(59) = ((k*(Ti(58)-Ti(59)) + k*(Ti(72)-Ti(59)) + k*(Ti(60)-Ti(59)) + k*(Ti(46)-Ti(59)))*(dt/Zinternal)) + Ti(59);
                T(60) = ((k*(Ti(59)-Ti(60)) + k*(Ti(73)-Ti(60)) + k*(Ti(61)-Ti(60)) + k*(Ti(47)-Ti(60)))*(dt/Zinternal)) + Ti(60);
                T(61) = ((k*(Ti(60)-Ti(61)) + k*(Ti(74)-Ti(61)) + k*(Ti(62)-Ti(61)) + k*(Ti(48)-Ti(61)))*(dt/Zinternal)) + Ti(61);
                T(62) = ((k*(Ti(61)-Ti(62)) + k*(Ti(75)-Ti(62)) + k*(Ti(63)-Ti(62)) + k*(Ti(49)-Ti(62)))*(dt/Zinternal)) + Ti(62);
                T(63) = ((k*(Ti(62)-Ti(63)) + k*(Ti(76)-Ti(63)) + k*(Ti(64)-Ti(63)) + k*(Ti(50)-Ti(63)))*(dt/Zinternal)) + Ti(63);
                T(64) = ((k*(Ti(63)-Ti(64)) + k*(Ti(77)-Ti(64)) + k*(Ti(65)-Ti(64)) + k*(Ti(51)-Ti(64)))*(dt/Zinternal)) + Ti(64);
                T(65) = ((k*(Ti(64)-Ti(65)) + (k/2)*(Ti(78)-Ti(65)) + h*Ay*(Tinf-Ti(65)) + (k/2)*(Ti(52)-Ti(65)))*(dt/Zedge)) + Ti(65);
                T(66) = (((k/2)*(Ti(79)-Ti(66)) + k*(Ti(67)-Ti(66)) + (k/2)*(Ti(53)-Ti(66)))*(dt/Zedge)) + Ti(66);
                T(67) = ((k*(Ti(66)-Ti(67)) + k*(Ti(80)-Ti(67)) + k*(Ti(68)-Ti(67)) + k*(Ti(54)-Ti(67)))*(dt/Zinternal)) + Ti(67);
                T(68) = ((k*(Ti(67)-Ti(68)) + k*(Ti(81)-Ti(68)) + k*(Ti(69)-Ti(68)) + k*(Ti(55)-Ti(68)))*(dt/Zinternal)) + Ti(68);
                T(69) = ((k*(Ti(68)-Ti(69)) + k*(Ti(82)-Ti(69)) + k*(Ti(70)-Ti(69)) + k*(Ti(56)-Ti(69)))*(dt/Zinternal)) + Ti(69);
                T(70) = ((k*(Ti(69)-Ti(70)) + k*(Ti(83)-Ti(70)) + k*(Ti(71)-Ti(70)) + k*(Ti(57)-Ti(70)))*(dt/Zinternal)) + Ti(70);
                T(71) = ((k*(Ti(70)-Ti(71)) + k*(Ti(84)-Ti(71)) + k*(Ti(72)-Ti(71)) + k*(Ti(58)-Ti(71)))*(dt/Zinternal)) + Ti(71);
                T(72) = ((k*(Ti(71)-Ti(72)) + k*(Ti(85)-Ti(72)) + k*(Ti(73)-Ti(72)) + k*(Ti(59)-Ti(72)))*(dt/Zinternal)) + Ti(72);
                T(73) = ((k*(Ti(72)-Ti(73)) + k*(Ti(86)-Ti(73)) + k*(Ti(74)-Ti(73)) + k*(Ti(60)-Ti(73)))*(dt/Zinternal)) + Ti(73);
                T(74) = ((k*(Ti(73)-Ti(74)) + k*(Ti(87)-Ti(74)) + k*(Ti(75)-Ti(74)) + k*(Ti(61)-Ti(74)))*(dt/Zinternal)) + Ti(74);
                T(75) = ((k*(Ti(74)-Ti(75)) + k*(Ti(88)-Ti(75)) + k*(Ti(76)-Ti(75)) + k*(Ti(62)-Ti(75)))*(dt/Zinternal)) + Ti(75);
                T(76) = ((k*(Ti(75)-Ti(76)) + k*(Ti(89)-Ti(76)) + k*(Ti(77)-Ti(76)) + k*(Ti(63)-Ti(76)))*(dt/Zinternal)) + Ti(76);
                T(77) = ((k*(Ti(76)-Ti(77)) + k*(Ti(90)-Ti(77)) + k*(Ti(78)-Ti(77)) + k*(Ti(64)-Ti(77)))*(dt/Zinternal)) + Ti(77);
                T(78) = ((k*(Ti(77)-Ti(78)) + (k/2)*(Ti(91)-Ti(78)) + h*Ay*(Tinf-Ti(78)) + (k/2)*(Ti(65)-Ti(78)))*(dt/Zedge)) + Ti(78);
                for j=1:numel(t)
                    T(79)=X(j);
                    T(80)=X(j);
                    T(81)=X(j);
                    T(82)=X(j);
                    T(83)=X(j);
                    T(84)=X(j);
                    T(85)=X(j);
                    T(86)=X(j);
                    T(87)=X(j);
                    T(88)=X(j);
                    T(89)=X(j);
                    T(90)=X(j);
                    T(91)=X(j);
                end
                Ti = T;
                Tfinal(:,1) = T0;
                Tfinal(:,i) = Ti;
        end

Subject: Coding error

From: Darren Rowland

Date: 29 Sep, 2010 02:15:32

Message: 6 of 8

> % Calculates the temperature at each node over the specified time
> ivals = 0:dt:20;
> for i = 1:length(ivals)
> T = Ti;
> T(1) = (((k/2)*(Ti(14)-Ti(1)) + (k/2)*(Ti(2)-Ti(1)) + h*Ax*(Tinf-Ti(1)))*(dt/Zcorners)) + Ti(1);
> T(2) = (((k/2)*(Ti(1)-Ti(2)) + k*(Ti(15)-Ti(2)) + (k/2)*(Ti(3)-Ti(2)) + h*Ax*(Tinf-Ti(2)))*(dt/Zedge)) + Ti(2);
...<snip>...
> T(78) = ((k*(Ti(77)-Ti(78)) + (k/2)*(Ti(91)-Ti(78)) + h*Ay*(Tinf-Ti(78)) + (k/2)*(Ti(65)-Ti(78)))*(dt/Zedge)) + Ti(78);
> for j=1:numel(t)
> T(79)=X(j);
> T(91)=X(j);
> end
> Ti = T;
> Tfinal(:,1) = T0;
> Tfinal(:,i) = Ti;
> end

Your code is not making any use of Matlab's true capabilities, and as such it will be very difficult to track down errors. Consider this example

                for j=1:numel(t)
                    T(79)=X(j);
                    T(80)=X(j);
                    ...
                    T(91)=X(j);
                end

This can be condensed to

for j = 1:numel(t)
    T(79:91) = X(j);
end

But it is clear that T is being overwritten each cycle through the loop so the only value of j which matters here is the last one, therefore this is equivalent to

T(79:91) = X(numel(t));

This is undoubtedly NOT what you want.
With proper use of indexing you could reduce most of your script to just a few lines, then you will probably catch the errors.

Hth,
Darren

Subject: Coding error

From: SF88

Date: 29 Sep, 2010 03:57:04

Message: 7 of 8

Darren, you're right I'm not using MATLab's capabilities, however I am capable of shortening my script as you suggested but to get the initial values I'm after I was simply copy pasting a lot of script and was intending to shorten it once it was complete. I'm still unsure of how I can make T(79) to T(91) equal the values I want them to over the 401 time steps. It still says they're simply 90 degrees for the duration, but I want them to start at 20 and graduate to 90. Any suggestions?

Subject: Coding error

From: Darren Rowland

Date: 29 Sep, 2010 05:23:21

Message: 8 of 8

"SF88 " <blah@hotmail.com> wrote in message <i7udeg$9e7$1@fred.mathworks.com>...
> It still says they're simply 90 degrees for the duration, but I want them to start at 20 and graduate to 90. Any suggestions?

Try replacing

               for j=1:numel(t)
                    T(79)=X(j);
                    T(80)=X(j);
                    T(81)=X(j);
                    T(82)=X(j);
                    T(83)=X(j);
                    T(84)=X(j);
                    T(85)=X(j);
                    T(86)=X(j);
                    T(87)=X(j);
                    T(88)=X(j);
                    T(89)=X(j);
                    T(90)=X(j);
                    T(91)=X(j);
                end

with

T(79:91) = X(i);


Hth,
Darren

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