Creating a Loop to calculate 2 varibles and then repeat calculation with new variable values

조회 수: 3 (최근 30일)
Need to create a script that:
h1,h2 and VFull need to change after every loop. NewVolume, h1New and power should be recorded in a table or in the workspace.
%constants
d = 7.2;
al = 11500;
cd = 0.8;
ct = 0.5;
g = 9.807;
p = 1029;
%Area Of turbine = pi*r^2
A = 40.715;
%Variables that need to change after every loop
h1 = 14.05; %h1 should be replaced with h1New after every 1 loop
h2 = Height(4851:21600,:); %this data is from a data excel sheet
VFull = 161575; %VFull shoule be replaced by NewVolume after every 1 loop
for c=1:16749 %loop needs to be repeated 16759 times
%Discharge
discharge(c) = cd*A*(sqrt(2*g(h1-h2)))
V = discharge/A
%Power
Power = (1/2)*ct*p*A(V^3)
%New Volume Calculation
NewVolume = VFull - discharge
%New h1
h1New = (VFull - discharge)
end

채택된 답변

Jakob B. Nielsen
Jakob B. Nielsen 2019년 12월 11일
You want to index your outputs, such that every iteration becomes that iteration numbers index in an array, instead of having the output as singles.
Your script also had several mistakes in it with taking the square root of a number that could become negative (the abs function prevents this), and several other small things. I changed some things around and now it at least gives an output - play around with it a little and see if you cant get it to do what you want.
%constants
d = 7.2;
al = 11500;
cd = 0.8;
ct = 0.5;
g = 9.807;
p = 1029;
%Area Of turbine = pi*r^2
A = 40.715;
%Variables that need to change after every loop
h1 = 14.05; %h1 should be replaced with h1New after every 1 loop
h2 = Height(4851:21600,:); %this data is from a data excel sheet
VFull = 161575; %VFull shoule be replaced by NewVolume after every 1 loop
for c=1:16749 %loop needs to be repeated 16759 times
%Discharge
discharge(c) = cd*A*(sqrt(2*g*((abs(h1-h2(c)))))) ;
V = discharge(c)/A;
%Power
Power = (1/2)*ct*p*A*V^3;
%New Volume Calculation
NewVolume(c) = VFull - discharge(c);
VFull=NewVolume(c);
%New h1
h1New(c) = (VFull - discharge(c));
h1=h1New(c);
end

추가 답변 (0개)

카테고리

Help CenterFile 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