# Integrating a function and then plotting it's graph in Matlab

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.

• it's graph/it's value --> its graph, its value – Wolfgang Bangerth Nov 21 '15 at 12:14
• Hey Wolfgang! Thanks for your help but I don't think I'm getting what you are conveying to me. Could you help explaining a little more? – Lloyd Kizito Nov 21 '15 at 13:35
• Can you indicate which ODE you are solving? There are some issues with your code: 1. you need D for vector y but don't specify it, later you then redefine D which may introduce bugs if you are not careful. 2. Why do you manually increase j using k+1 in the loop? it seems to me simply using j=1:181 and M(j) works just fine. 3. It would be best not to use a for-loop and vectorize the call to integral 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... – nluigi Nov 21 '15 at 14:27
• @LloydKizito -- I'm simply pointing out a persistent grammar error in your question. – Wolfgang Bangerth Nov 22 '15 at 1:04

I think the main problem you encounter is that your for-loop probably doesn't do what you expect.

k = 1;
M =zeros(1,181);
for j = k:181
M(k) = integral(@(y) Func5(p,D,k,y),0,D(k));
k+1;  # does not increase k
end


The problem here is that the statement k+1 is useless and does not increase k which i think is what you are after. The problem with this is that j is still increased each iteration but the values of the integral are assigned to M(k) which remain M(1) rather than increase with the loop. Also the argument D(k) remains unchanged which i guess should instead change with the iteration aswell.

M =zeros(1,181);

Some general advice about your code: try using comments which add useful information to improve readability. M =zeros(1,181); %an array is not a useful comment because i can already see this is 'an array', what i cant see is what M means; a better comment would be % preallocate the results vector , the same applies for D =10:0.5:100; %Create a vector for D; what is D? and for that matter all other variables? Often it is also better to use meaningful variable names like results or pressure rather than D and M. Furthermore, in a function like Func5 it is useful to include a larger comment block at the top to say what the function calculates (which equation) and what its in/outputs are.