a = xlsread('outdoorall.xlsx','H2:H52364');
b=xlsread('indoorall.xlsx','H2:H52364'); %2397
t=xlsread('outdoorall.xlsx','E2:E52364');% time in secs
for i=1:2363
dti=t(i+1)-t(i);
dt=(b(i+1)-b(i))/dti; % change in indoor temperature divided by time
c=dt/a(i)-b(i);
plot(c)
end
the idea is each time step is different so i want to divide the change in temp by each unique time step

5 个评论

So what is your question?
As c is scalar the plot will look empty (it isn't actually, as it contains one line with one point and no marker). All loop iterations are entirely replaced by the next iteration, so only the last one will remain plotted in the axes (all one invisible point of it!).
Probably you should plot after the loop, then you will actually see something.
still no plot
aim is to work out the difference between each time interval then divide the subsequent difference from dt to produce a graph
Copy of question
a = xlsread('outdoorall.xlsx','H2:H52364');
b=xlsread('indoorall.xlsx','H2:H52364'); %2397
t=xlsread('outdoorall.xlsx','E2:E52364');% time in secs
for i=1:2363
dti=t(i+1)-t(i);
dt=(b(i+1)-b(i))/dti; % change in indoor temperature divided by time
c=dt/a(i)-b(i);
plot(c)
end
the idea is each time step is different so i want to divide the change in temp by each unique time step
___________________________________________________________
My comment
Try
c = nan(1, 2363);
for i=1:2363
dti=t(i+1)-t(i);
dt=(b(i+1)-b(i))/dti; % change in indoor temperature divided by time
c(i)=dt/a(i)-b(i);
end
plot(c)
or
hold on
for i=1:2363
dti=t(i+1)-t(i);
dt=(b(i+1)-b(i))/dti; % change in indoor temperature divided by time
c=dt/a(i)-b(i);
plot(c, 'o')
end
(Answers Dev) Restored edit

请先登录,再进行评论。

 采纳的回答

Hank
Hank 2020-2-11
编辑:Hank 2020-2-11
Do this without a for loop
% Load data
a = xlsread('outdoorall.xlsx','H2:H52364');
b=xlsread('indoorall.xlsx','H2:H52364'); %2397
t=xlsread('outdoorall.xlsx','E2:E52364');% time in secs
a = a(:); b = b(:); t = t(:); % make sure we're working with column vectors
dti = diff(t) % difference between each value of t, length is one less than 1;
% it looks like your formula is the difference in db/dt divided by the difference between a and b.
% Pad db/dt with 0 so the array length matches with a and b.
c = [0; diff(b)./dt] ./ (a-b) ;
plot(t,c)
is this what you're trying to do?

4 个评论

+1, but with some changes.
  1. dt needs to be dti
  2. Instead of padding the 0 in c, remove the last values in a and b to match what the loop is doing. Note, it may be the case that the padding is actually what the OP intended to do and it would preserve the number of data points.
  3. In the loop, c=dt/a(i)-b(i); dt is divided by a, not divided by (a-b). Again, the OP should carefully decide which is needed.
a = a(:); b = b(:); t = t(:);
dti = diff(t)
c = (diff(b)./dti) ./ a(1:end-1) - b(1:end-1) ;
plot(c)
% or
% plot(t(1:end-1),c)
Compare results
% OP's loop (cleaned up)
t = rand(400,1);
a = rand(400,1);
b = rand(400,1);
c = nan(1, numel(t));
for i=1:numel(t)-1
dti=t(i+1)-t(i);
dt=(b(i+1)-b(i))/dti; % change in indoor temperature divided by time
c(i)=dt/a(i)-b(i);
end
clf()
subplot(2,1,1)
plot(c)
title('OP''s Loop')
% Vectorized version
dti = diff(t)
c = (diff(b)./dti) ./ a(1:end-1) - b(1:end-1) ;
subplot(2,1,2)
plot(c)
title('Vectorization')
I'm a bit confused now, this is the eq i'm trying to solve.
I need it to be consecutive values so i dont think rand would be useful to me.
so which of these codes would work?
Also why is it to end-1 and not end?
"I need it to be consecutive values so i dont think rand would be useful to me."
You did not provide a, b, or t, so Adam Danz quite reasonably just used some matrices of random values in order to actually run the code on something.
You should use your own arrays, of course, not the random values that are just used for testing.
apologies, thought i attached the data

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by