Search code examples
matlabmatlab-figure

MATLAB - plot an iteration


The code so far:

function [fr]=frictionFactorFn(rho,mu,e,D,L,Q,f0,tol,imax)
format long
CS=(pi*D^(2))/4;%Cross sectional area of pipe
v=Q/CS;%velocity
Re=(rho*v*L)/mu;
iter=1;i=1;fr(1)=f0;
while 1
fr(i+1)=(-1.74*log((1.254/(Re*sqrt(fr(i))))+((e/D)/3.708)))^-2;%substitution for root finding
iter=iter+1;
if abs(fr(i+1)-fr(i))<tol || iter>=imax
break;
end
i=i+1;
end
fprintf('\n The Reynolds number is %f\n',Re);
plot(0:iter-1,fr);
xlabel('Number of iteration'),ylabel('friction factor');
end

It gave me the right converged value of f=0.005408015, but I would like to plot the iteration


Solution

  • Possibly by storing the values of f upon each iteration in an array. In this example the array is called Store_f and is plotted after the while-loop is completed. The variable Index below is used to indicate which cell of array Store_f the value should be saved to.

    Plotting Function

    function [f_vals] = frictionfactorfn()
    
    Index = 1;
    
        while (Condition)
        %Calculation code%
    
            Store_f(Index) = f;
            Index = Index + 1;
        end
    
        disp(num2str(f))
        plot(Store_f,'Marker','.');
        xlabel('Iteration'); ylabel('Value');
    end