I am struggling to get started on this MATLAB question. Any tips on how to use for loops in this? Or important things to take note of?

1 visualización (últimos 30 días)

Respuesta aceptada

Image Analyst
Image Analyst el 16 de Sept. de 2021
Editada: Image Analyst el 16 de Sept. de 2021
Do you know how to program in the MATLAB language yet?
Here's a start
sa = 32760; % Surface area
wallHeight = 26;
data = readmatrix('data_river_discharge.dat');
inputRate = data(:, 1); % Column 1 is the river input charging rate (into the lake).
outputRate = data(:, 2); % Column 2 is the discharge rate from the dam (out of the lake).
numTimePeriods = size(data, 1); % Number of rows. Each row is a different time point.
for k = 1 : numTimePeriods
% Code goes here
if waterLevel > 0.8 * wallHeight
% Goes over spillway.
Qout =
elseif waterLevel < 0.3 * wallHeight
% Reduce outflow
Qout =
else
% Normal operating condition.
end
% Get water level at end of this time period
waterLevel = something involving prior water level and Qout and Qin
end
  7 comentarios
Image Analyst
Image Analyst el 17 de Sept. de 2021
When you do this:
y(i)=y(i-1)+((Qin(i-1))/sa)-(Qout(i-1)/sa)
p=y/wallHeight
you're setting the ith element of y in the first equation, so y is a vector. In the next line, you're using the entire y (all elements) so p will not be a single number, but a whole vector of numbers, as I'm sure you found out when you stepped through the code. So p < 0.3 will also be a logical vector, not a single true or false value but a whole array of them.
Also
if 0.8>p>0.3
is not allowed. You have to do it this way, with 2 comparisons
if p > 0.3 && p < 0.8
If you do this:
for i=2
then the loop will only iterate once. Setting i to a new value at the end of the loop does not change the fact that it will still only go through once. The format is
for k = startingValue : incrementValue : endingValue
Looks like you first need to spend 2 hours with this:
Ned Williams
Ned Williams el 18 de Sept. de 2021
clc;close;clear
format short g
data=xlsread('data_RiverDischarge.xls');
sa=352760;
H=26;
vol=sa*H;
Rin=data(:,1);
days=size(data,1);
y(1)=18.8;
Qin=data;
Qout=5.33;
Qr=2.2*Qout;
for i=2:1:days
I(i-1)=(Qin(i-1))/sa
O(i-1)=(Qout(i-1))/sa
R(i-1)=(Qr(i-1))/sa
y(i)=y(i-1)+I(i-1)-O(i-1)-R(i-1)
p=y(i)/H
for Q=Qout
if p>0.3 && p<0.8
Q=5.33; Qr=0
elseif p>0.8
Q=5.33;Qr=2.2*Qo
elseif p<0.3
Q=(1/1)*Qin(i)
else p>=1
disp('overflow')
end
end
y(i+1)=y(i)+I(i)-O(i)-R(i)
p=(y(i+1))/H
end
Does this make any more sense> I am still getting an error saying that the Index exceeds the number of array elements (1)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by