I've been working on this code for some hours and it seems I'm doing something wrong which I just can't figure out. I have a function which I integrate and then plot it's values. But the graph I get looks strange. I expected some form of an exponential function but the one I get is far away from my anticipations, and when I try changing some of my constants, it does still behave in a strange manner. This is how the code looks like:
y = 0:0.5:D;
p = 65.5;
k = 1;
D =10:0.5:100; %Create a vector for D
M =zeros(1,181); %an array
for j = k:181 %For loop for all water values
M(k) = integral(@(y) Func5(p,D,k,y),0,D(k));
k+1;
end
plot(D,M)
title('Hydrostatic pressure in bars');
xlabel('water level in fot');
ylabel('pressure i bars');
And this is the code for my function:
function FD = Func5(p,D,k,y)
w = 40-20*exp(-(0.01*y))*2; %Width of the dam
FD = p.*(D(k)-y).*w; %Claculate the pressure
end
Thanks in advance for any help.
D
for vectory
but don't specify it, later you then redefineD
which may introduce bugs if you are not careful. 2. Why do you manually increasej
usingk+1
in the loop? it seems to me simply usingj=1:181
andM(j)
works just fine. 3. It would be best not to use a for-loop and vectorize the call tointegral
but since i am not sure what you are solving, it is difficult to say if that is possible. 4. It is unclear what the different variables (D, k, etc) are... $\endgroup$ – nluigi Nov 21 '15 at 14:27