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:
store value in loop

Subject: store value in loop

From: Azrul

Date: 8 Mar, 2011 16:16:19

Message: 1 of 3

hi all,

I have one simple matlab coding that uses for loop. The coding is :

t1=25;
t2=25;
ta=25;

for n=0:0.05:1440;
    t1=t1+0.054439*((t2-t1)*(1.045871)+(ta-t1)*(7.2381));
end

The problem is, i'm not sure how to save all data t1 from n=0 to n=1440 . All I have is the value of t1 WHEN n=1440 ie the last value as it's overwriting the prev value. I know it just a simple 1-2 lines coding but I just couldn't find one on the net. Thank you in advance

Subject: store value in loop

From: Steven_Lord

Date: 8 Mar, 2011 17:58:17

Message: 2 of 3



"Azrul " <azrul.afifi@gmail.com> wrote in message
news:il5koj$6ac$1@fred.mathworks.com...
> hi all,
>
> I have one simple matlab coding that uses for loop. The coding is :
>
> t1=25;
> t2=25;
> ta=25;
>
> for n=0:0.05:1440;
> t1=t1+0.054439*((t2-t1)*(1.045871)+(ta-t1)*(7.2381));

At the first iteration through your loop, (t2-t1) and (ta-t1) are both 0 and
so t1 doesn't change. That means that at the next iteration, (t2-t1) and
(ta-t1) are still 0 and so t1 will never change.

> end
>
> The problem is, i'm not sure how to save all data t1 from n=0 to n=1440 .
> All I have is the value of t1 WHEN n=1440 ie the last value as it's
> overwriting the prev value. I know it just a simple 1-2 lines coding but I
> just couldn't find one on the net. Thank you in advance

You know how long you want t1 to be.

nVector = 0:0.05:1440;
t1 = zeros(numel(nVector)+1, 1);
for whichN = 1:numel(nVector);
    t1(whichN+1) = t1(whichN) + ...
end

Alternately, the FILTER or CUMSUM functions may do what you want without a
loop.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: store value in loop

From: Azrul

Date: 8 Mar, 2011 22:10:21

Message: 3 of 3

Actually there are 10 more functions which is T1,T2,......,T10 . The first iteration will change the value of T10 and so on till T1 (means T1 will change at certain time) . I only put T1 for simplicity. Maybe I should make it more clearer in the question . I've tried what you told me to do and it works. Thank you.

"Steven_Lord" <slord@mathworks.com> wrote in message <il5qn6$9kp$1@fred.mathworks.com>...
>
>
> "Azrul " <azrul.afifi@gmail.com> wrote in message
> news:il5koj$6ac$1@fred.mathworks.com...
> > hi all,
> >
> > I have one simple matlab coding that uses for loop. The coding is :
> >
> > t1=25;
> > t2=25;
> > ta=25;
> >
> > for n=0:0.05:1440;
> > t1=t1+0.054439*((t2-t1)*(1.045871)+(ta-t1)*(7.2381));
>
> At the first iteration through your loop, (t2-t1) and (ta-t1) are both 0 and
> so t1 doesn't change. That means that at the next iteration, (t2-t1) and
> (ta-t1) are still 0 and so t1 will never change.
>
> > end
> >
> > The problem is, i'm not sure how to save all data t1 from n=0 to n=1440 .
> > All I have is the value of t1 WHEN n=1440 ie the last value as it's
> > overwriting the prev value. I know it just a simple 1-2 lines coding but I
> > just couldn't find one on the net. Thank you in advance
>
> You know how long you want t1 to be.
>
> nVector = 0:0.05:1440;
> t1 = zeros(numel(nVector)+1, 1);
> for whichN = 1:numel(nVector);
> t1(whichN+1) = t1(whichN) + ...
> end
>
> Alternately, the FILTER or CUMSUM functions may do what you want without a
> loop.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

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