writing a formula dependant on time

I'm trying to write a simple formula for the following code:
if true
%variables
H0=10;
w=(2*pi)/12.25;
Qm1=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
Qm2=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
Qm12=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
t=0.5:0.5:24;
A1=101364;
A2=A1;
tau=0.5;
rho=1029;
N=0.8;
g=9.81;
H1(1)=10;
H2(1)=0;
d1=1;
d2=0;
d3=0;
%Heads
%Ocean
Hm=H0*(cos(w*t));
%Basin1
H1(t)=(H1*(t-tau))-((Qm1/A1)*tau)+((Qm12/A1)*tau);
%Basin2
%H2(t)=H2*(t-tau)+(Qm2/A2)*tau-(Qm12/A2)*tau;
%Power
P1=N*rho*(Hm-H1)*Qm1*d1;
P2=N*rho*g*(Hm-H2)*Qm2*d2;
%P3=N*rho*g*(Hm-H12)*Qm12*d3;
P=P1+P2+P3;
end
P1,P2 and P3 fail since that Matlab recognises I'm trying to multiply two 1x48 matrices. Hm and H1 are fine, however Q1 and Q2 should represent different values throughout a daily cycle (48 values for 48 half hours in a day). So should I end with 48 values for P1 P2 and P3 each. What I'm trying to write is a formula for P(t) and for Matlab to repeat the formula 48 times to show me how it changes through the day.
I'm also not sure my code for H11 is right. It should represent an instantaneous value:
if true
H1(t)=(H1*(t-tau))-((Qm1/A1)*tau)+((Qm12/A1)*tau)
end
So H1 is equal to the value of H1 the half hour before minus the value for Qm1/A1 for that instance multiplied by tau, which is half an hour. The Q values will eventually be populated by values once I know the code is working
I would appreciate an answer as to how I would code this correctly and I apologise for the unclear explanation. Please ask any questions and I'll answer to the best of my ability.

3 Comments

If you have a phone with camera, capture the photo of formula you want to code up and upload it here.
This a duplicate of your previous Question: http://www.mathworks.com/matlabcentral/answers/269463-how-do-i-solve-inner-matrix-must-agree-in-this-question. The same observations and comments apply here.
Go through your code to find the reason one of your vectors is 97 elements long and the other is 48 elements long.
I cannot determine what you want your code to do, and what result you want from it in either your previous Question or this one.
Not quite the same as the last one, though the code sure does look the same. My matrices now are correct thanks to your advice. This is more about making sure the matrices are doing what I need.
I'll upload a photo with this message of the code in its most basic form. I basically want the code to run through the first set of date, then repeat for the second and so on until the 48th, so I know values for P at every half an hour.

Sign in to comment.

Answers (1)

Use element wise operators (.* and ./) rather than matrix operators (* and /)
A = magic(2)
A*A
A.*A
A/A
A./A

1 Comment

Thanks for your reply but it's not quite what I was looking for. Could you take a look at the photo attached and maybe explain how I would go about coding it so for every half an hour the equation inputs the values for Q, Hm and H1 and outputs a value for P so the complete code displays a set of 48 values.

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 23 Feb 2016

Commented:

on 23 Feb 2016

Community Treasure Hunt

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

Start Hunting!