I'm using the moody function
by ILDEBERTO DE LOS SANTOS RUIZ and newton method for solving colebrook white equation.
My code is
a0 = input('Enter initial guess: ');
e = input('Tolerable error for: ');
n = input('Enter maximum number of steps: ');
a2=0;
format short
for Q=[0.0001,0.0002,0.0005,0.001,0.002,0.005,0.01,0.02]
for Re=[10000,20000,50000,100000,200000,500000,1000000]
syms f
eq1=(-0.869*log((Q/3.7) + (2.523/(Re*sqrt(f)))))*(sqrt(f))-1;
g = diff(eq1);
g2= diff(g);
fa = eval(subs(eq1,f,a0));
ga = eval(subs(g,f,a0));
ga2 = eval(subs(g2,f,a0));
gg=abs((fa*ga2)/ga^2);
an=a0-(fa/ga);
if gg<1
disp('Convergent')
end
if fa~=0 && ga~=0
for i=1:n
a1= an;
fprintf('x%d = %.8f\n',i,a1)
[numRows, numCols] = size(a2);
a2(1, numCols+1) = a1;
a1= an;
if abs(a1-a0)<e
break
end
a0=a1;
end
end
moody(Q,Re) %moody function the part Im having problem
end
end
Where Q is epsilon/D
I want to plot all my Q and Re that are in the loop on the same figure where moody diagram is
I tried the hold on after the moody function but it showed me the same plot in the figure
EDIT:
I changed the code and now I get diffrent figures saved to pc each time and I made them into a gif but I need it to be on same plot
Related
I'm trying to convert this script from matlab to scilab. I tried the scilab tool for conversion but it crashes with no output so I'm doing it by hand. This is what I got by changing the comments from % to //. #markovprocessfunc refers to another file I was able to convert with the scilab automatic tool.
// Markovprocess
// Main program of the markovprocess simulation
// The system is defined by the generator matrix A
// Ulf Jeppsson, January 2017
global B
// Define the duration of the simulation
disp(' ')
disp('Automation 2017, Markov processes')
disp(' ')
tend = input('Set simulation time = ');
disp(' ')
// Make a new simulation with the same A matrix?
yes = input('Keep the old A matrix? (yes=1, no=0) ');
disp(' ')
if yes>0.9
A
else
disp('Provide the A generator matrix (on the form [ x x .. ; x ...], or give the name of a predefined matrix in the Matlab workspace)')
A = input('A = ')
end
// The generator matrix A is transposed to a matrix B
B = A';
//define the initial condition p(0) (a row vector)
p = input('Define initial condition, p (row) vector (on the form [ x x .. ]) ');
// The row vector p is transposed to a column vector x0
x0 = p';
//Solve the differential equation
[t,x] = ode45(#markovprocessfunc,[0,tend],x0);
// Plot the results
plot(t,x)
grid on;
xlabel('Time')
ylabel('Probabilities')
m=length(A);
if m==2
legend('s1', 's2')
end
if m==3
legend('s1', 's2','s3')
end
if m==4
legend('s1', 's2','s3', 's4')
end
if m==5
legend('s1', 's2','s3', 's4', 's5')
end
if m==6
legend('s1', 's2','s3', 's4', 's5', 's6')
end
if m==7
legend('s1', 's2','s3', 's4', 's5', 's6', 's7')
end
if m==8
legend('s1', 's2','s3', 's4', 's5', 's6', 's7', 's8')
end
title('Markov Process')
m = size(A,1);
aa = A'; //transpose the A matrix
aa(m,:) = 1; //add ones to the last column (can be any column), i.e. the sum all p(i) must equal 1
b = zeros(m,1); //create b-vector with zeros
b(m)=1; //add a 1 to the same row as where you added 1:s to the column of A
c = aa\b; //solve the linear equation system
stationary_solution_vector = c' //transpose the solution vector back to a row vector
e_raised_to_A_times_1000 = expm(A*1000)
eigenvalues_of_A = eig(A)'
I found out that by replacing the line [t,x] = ode45(#markovprocessfunc,[0,tend],x0); with something like
t = [0 1 2 3]
x = [1 2 3 4]
the script works fine (but obviously I get the wrong result). So how do I convert ode45 to scilab? I think that after I get that done everything will work.
About the crash of mfile2sci() code converter, please do not hesitate to post a bug report # https://bugzilla.scilab.org for your user case. Things can't get better without reporting bugs.
Even without converter's crash, the ode45() instruction is not converted. But it could be (let us put this on our todo list ;-). Indeed, an equivalent of
[t,x] = ode45(#markovprocessfunc,[0,tend],x0);
is
n = 100; // or whatever value you wish
t = linspace(0, tend, n);
x = ode("rkf", x0, t(1), t, markovprocessfunc);
I've already tried to change x, y, z but it still does not work.
The error said that there is problem in
(line 72) surf(t/sigma,x*sqrt(P/sigma)/sqrt(phi),abs(Y(:,1:M)));
My full code is:
clc
M = input ('M = ');
N = input ('N = ');
epsilon = input ('epsilon = ');
dx = 0.01;
dt = 2.5000e-005;
x0 = -N/2*dx;
r = dt/dx^2;
D = 0.1*1.6e5;
mass=5.1e-13;
a=3e-2;
k=24;
K=8;
l=340;
lambda=10*1;
q=2*pi/lambda;
h=4;
sigma=1e10;
tau=0;
e2=0.001;
wg = 2*a*sqrt(D/mass);
w = sqrt(wg+(2*k*(1-cos(q*1))+2*K*(cos(q*l*h)+1))/mass);
Vg=(1*(k*sin(1*q)-K*h*sin(l*q*h))/(mass*w));
alfa = (-3*a)/sqrt(2);
beta = 7*a^2/3;
miu=-2*alfa/(1+4*K/ (mass*wg.^2));
delta=(wg.^2)*alfa/(4*w^2+2*k*(cos(2*q*1-1)/mass-2*K*(cos(2*h*q*l)+1)/mass-wg^2));
P=(((1^2)*(k*cos(1*q)-K*(h^2)*cos(l*q*h))/mass)-Vg^2)/(2*w);
Q=(-wg.^2*(2*alfa*(miu+delta)+3*beta)/(2*w));
phi= 0.0005;
psi=0.03;
F=zeros(N,M);
Y=zeros(N,M);
for n=1:N
x(n,1)=x0+(n-1)*dx;
F(n,1)=((sqrt(2)/psi)*sech(x(n,1))/sqrt(phi));
end
%running program
for m=1:M
for n=2:N-1
if n==2
F(n-1,m)=3*F(n,m)-3*F(n+1,m)+F(n+2,m);
end
if n==N-1
F(n+1,m)=3*F(n,m)-3*F(n-1,m)+F(n-2,m);
end
if m==1
F(n,m+1)=1i*((phi^2*r*(F(n+1,m)-2*(F(n,m))+F(n-1,m))+(psi^2*dt*(conj(F(n,m))*(F(n,m)^2))+F(n,m))));
else
F(n,m+1)=2*1i*((phi^2*r*(F(n+1,m)-2*(F(n,m))+F(n-1,m))+(psi^2*dt*(conj(F(n,m))*(F(n,m)^2))+F(n,m-1))));
end
t(m) = (m-1)*dt;
theta(n,m)=(n*q*l)-(w*t(m)/e2);
Y(n,m) = ((e2*(psi*F(n,m)/(sqrt(Q/sigma))))*(2*(cos((sigma*tau)+theta(n,m)))))+((e2^2*((psi*F(n,m)/(sqrt(Q/sigma)))^2))*(miu+(2*delta*(cos(2*((sigma*tau)+theta(n,m)))))));
end
%grafik 3 dimensi
figure
surf(t/sigma,x*sqrt(P/sigma)/sqrt(phi),abs(Y(:,1:M)));
view(0,90);
colorbar
shading interp
xlabel ('T');
ylabel ('x');
zlabel ('y');
%grafik hubungan Y terhadap x
figure
plot((x*sqrt(P/sigma)),abs(Y(:,0)));
xlabel ('x(pm)');
ylabel ('Y(pm)');
figure
plot((x*sqrt(P/sigma)),abs(Y(:,M)));
xlabel ('x(pm)');
ylabel ('Y(pm)');
It makes the figure not shown. Please what should I do?
Taking Real Components and MATLAB Indexing
To avoid errors referring to imaginary components add the real() function to only use the real components while surface plotting:
surf(t/sigma,real(x*sqrt(P/sigma)/sqrt(phi)),abs(Y(:,1:M)));
MATLAB indexing begins at "1" rather than the "0" that many languages use. Changing abs(Y(:,0)) to abs(Y(:,1)) may sole the problem.
plot((real(x*sqrt(P/sigma))),abs(Y(:,1)));
Adding additional end to close the nested for-loop.
for m=1:M
for n=2:N-1
if n==2
F(n-1,m)=3*F(n,m)-3*F(n+1,m)+F(n+2,m);
end
if n==N-1
F(n+1,m)=3*F(n,m)-3*F(n-1,m)+F(n-2,m);
end
if m==1
F(n,m+1)=1i*((phi^2*r*(F(n+1,m)-2*(F(n,m))+F(n-1,m))+(psi^2*dt*(conj(F(n,m))*(F(n,m)^2))+F(n,m))));
else
F(n,m+1)=2*1i*((phi^2*r*(F(n+1,m)-2*(F(n,m))+F(n-1,m))+(psi^2*dt*(conj(F(n,m))*(F(n,m)^2))+F(n,m-1))));
end
t(m) = (m-1)*dt;
theta(n,m)=(n*q*l)-(w*t(m)/e2);
Y(n,m) = ((e2*(psi*F(n,m)/(sqrt(Q/sigma))))*(2*(cos((sigma*tau)+theta(n,m)))))+((e2^2*((psi*F(n,m)/(sqrt(Q/sigma)))^2))*(miu+(2*delta*(cos(2*((sigma*tau)+theta(n,m)))))));
end
end
I have to fit the data of a given equation to other data in the blue line shown in picture attached.
The initial values for r and c are 0.004 and 40 respectively. The result values for r and c will be 0.0068 and 54.23 respectively. The equation are provided below.
I am not getting the fit graph. I don't know where I am making a mistake. Red circles are I think my fitting result. Here is my working on Matlab.
Function file:
function y = FOM2(t,r,c)
t=(0:0.01:2);
q=zeros(1,length(t));
for i=1:length(t)
if (t(i) < 1)
q(i) = 600;
else
q(i) = 0;
end
y= q*(r+(1/c));
end
Error value function:
function error=RCvalues(x,time,data)
r = x(1); c = x(2);
test = FOM2(time,r,c);
d= data - test;
error = sum(d.^2);
Fitting file:
Data = load ('Paw.txt'); %Load measurement data
Time = load ('time.txt');
Data=data';
Time=ime';
opt = optimset('MaxFunEvals',10000,'TolFun',1e-6);
x = fminsearch(#(x) RCvalues(x,Time,Data),[0.004 40],opt);
r = x(1);
c = x(2);
yy= FOM2(Time,r,c);
fprintf('Final values:\n');
fprintf('r %7.3f\n',x(1));
fprintf('c %7.3f\n',x(2));
plot(Time,Data)
hold on
plot(Time,yy,'o');
Here are the equations,
totaltime is 2 sec
dPalv/dt=q/c
Paw= r*q+palv
I have to use Paw equation but don't know how.
I want to integrate a differential equation dc/dt. Below is the code and the values of the variables.
clear all;
c1=.185;c0=2*10^-6;k3=.1*10^-6;
v1=6;v2=.11;v3=.09*10^-6;
Ca_ER=10*10^-6;Ca_cyto=1.7*10^-6;
p_open3=0.15;c=15*10^-6;
dcdt= (c1*(v1*(p_open3)+v2)*(Ca_ER)-c)-v3*((c)^2)/(c^2+(k3)^2);
I know there is an integral function but I am not sure how to apply for this equation. How do I proceed from here? Please help. The value of initial c, if needed, can be taken as 0.15*10^-6. Also, I need to plot the obtained result versus time. So will get an array of values or just a single value?
the link to the article. the equation i have used comes under Calcium Oscillations section
You could use Euler method to solve this problem to get a rough idea regarding the solution yet not accurate.
clear all
clc
t = 0;
dt = 0.0001;
c1 = 0.185;
c0 = 2*10^-6;
k3 = 0.1*10^-6;
v1 =6;
v2 =.11;
v3 =.09*10^-6;
Ca_ER =10*10^-6;
Ca_cyto =1.7*10^-6;
p_open3 =0.15;
c = 15*10^-6;
%store initial values
C(1) = c;
T(1) = t;
for i = 1:40000
dc = ( (c1*(v1*(p_open3)+v2)*(Ca_ER)-c)- v3*( c^2 /( c^2+(k3)^2) ) );
c = c + dt*dc;
t = t + dt;
%store data
C(i+1) = c;
T(i+1) = t;
end
plot(T,C, 'LineWidth',2)
xlabel('time (sec)')
ylabel('c(t)')
grid on
The result is
You can also use Wolfram which gives same result.
Hi I am trying to run a code for image reconstruction of 2D projections using the iradon transform. My aim to reduce the time significantly for which I am trying to using parallel for loop in my local cluster profile of my laptop.
But I seem to get an error which I am finding it difficult to debug-
Error: The variable data in a parfor cannot be classified.
My code-
clc;
close all;
clear all;
tic
% projection_length = input('Define projection length (px) = ');
projection_length = 4100; % which means our ouput will have 4100 cross sectional images of the capillary tube
parfor q = 1:projection_length
for i = 1:5 % typically 500, since we take 500 projections around the capillary tube 0.72 deg inc
if length(num2str(i)) == 1
data(q,:, i) = imread(['pre00' num2str(i) '.tif'], 'PixelRegion', {[1 1600], [q q]});
elseif length(num2str(i)) == 2
data(q,:, i) = imread(['pre0' num2str(i) '.tif'], 'PixelRegion', {[1 1600], [q q]});
elseif length(num2str(i)) == 3
data(q,:, i) = imread(['pre' num2str(i) '.tif'], 'PixelRegion', {[1 1600], [q q]});
end
disp(['Analyzing projection ' num2str(q) ' of ' num2str(projection_length) ', Angle ' num2str(i) '...']);
end
H = iradon( data(q,:,:), 0.72, 'Hann', 0.8, 1600);
end
toc
Firstly, I would recommend using sprintf to build the filename, like so:
fname = sprintf('pre%03d.tif', i);
data(q, :, i) = imread(fname, ...);
This will correctly zero-pad fname.
The problem with data is that you're indexing it in different ways, and unfortunately parfor cannot understand that correctly. (This should show up as a "red" warning in the MATLAB editor, which can sometimes give you better information about parfor problems). I would be tempted to fix the problem something like this:
parfor q = 1:projection_length
tmpData = zeros(N, numFiles); % insert correct value of N here
for i = 1:numFiles
fname = ...;
tmpData(:, i) = imread(fname, ...);
end
data(q, :, :) = tmpData;
H = iradon(tmpData, ...);
end
I'm not sure what you're trying to do with H - parfor will treat that as a temporary variable, and the value will not be available after the loop.