Solve equation of mean field theory with Scilab - scilab

i try to evaluate the equation =tanh(zJ/k_B T)
using newton rampson method. when i run the program i got an error:
plot: Wrong size for input arguments #2 and #3: Incompatible
dimensions.
and a blank graph. plz help me out whats the problem in my code.
z=4\\ no. of nearest neighbours
m=1;//value of J/K
T=[0.1:0.1:8]\\value of temerature
s(1)=-0.5;//initial value
n=100;\\no. of iterations
for i=1:n \\ running of for loop
f= s(i)-tanh(z*m*s(i)./T);//equation of mean field theory
l=derivat(f); // derivative of f
s(i+1)=s(i)-(f/l); // implementation of newton rampson method
if abs(s(i+1)-s(i)) <= 10^-8 then
end
s(i)=s(i+1)
i=i+1;//increment in i values
end
plot(T,s,'.r')

What I understand after reading about equation of mean field theory is that you want to solve s=tanh(zms/T) for different values of T then plot s versus T. Here is how you can do it in Scilab (no need to code Newton's method yourself you can use fsolve (see the help page of this function)
function out = eq(s,T)
out = s-tanh(z*m*s/T)
end
z=4;
m=1;
T=[0.1:0.1:8];
for i=1:length(T)
s(i) = fsolve(-0.5,list(eq,T(i)))
end
clf
plot(T,s)
xlabel T
ylabel s

Related

Simulation's problem in a fermentor with Monod kinetics

I am simulating a non-linear model (fermentor with Monod kinetics), but I found the output of this model is still fixed at the equilibrium points (initial conditions), Q is input and I used xcos to generate a step input (as a vector) with initial value=2, final value=2.2 and step time=20. I am going to attach my figure. Thanks in advance.
Output figure
funcprot(0)
t1=A.time
Q=A.values(:,1)
function dxdt=f(t,x)
S=x(1);
X=x(2);
dxdt=[(Q/20)*(0.02-S)-(0.4/0.67)*(S*X)/(0.015+S)
(-Q*X/20)+0.4*((S*X)/(0.015+S))]
endfunction
t=linspace(0,100,999);
x0=[0.005;0.0101];
x=ode(x0,0,t,f)
scf(0)
plot(t1,x(2,:))
xlabel('time')
ylabel('X')
ax=gca(),// gat the handle on the current axes
ax.data_bounds=[0 0;100 2.5]; //axis lim

Problem in solving ordinary differential equation in scilab

clf
clc
function dx=f(t,x)
dx(1) = x(2)
dx(2) = -B*(1-x^2)*x(2)-w*w*x(1)
endfunction
B = 5
w = 1
x0 = [10;100]
t = 0:0.01:10
x = ode(x0,0,t,f)
plot2d(t,x)
Now it shows the following problem in scilab console...
at line 3 of function f ( /Users/mottelet/so.sce line 7 )
in builtin ode
at line 15 of executed file /Users/mottelet/so.sce
Submatrix incorrectly defined.
ode: An error occurred in 'lsoda' subroutine.
Please tell me what did I do wrong and tell the solution.
You have a problem in the code of function f at this line
dx(2) = -B*(1-x^2)*x(2)-w*w*x(1)
because the expression -B*(1-x^2)*x(2) yields a vector and you try to assign it to a scalar container (here dx(2)). Maybe you wanted to write -B*(1-x(1)^2)*x(2) or -B*(1-x(2)^2)*x(2) (i.e. replace the first occurence of x by one of its components) ?

How to overcome "Variable returned by scilab argument function is incorrect" while using fsolve in scilab?

While solving one problem in the fluid mechanics topic, I came across a situation where I have to solve 4 non linear equation to get 4 unknown variable values. So, I used fsolve function in scilab to solve the equations. My code is as follows:
clc
clear
function f=F(x)
f(1)=x(1)-(0.4458*x(2)^(-2))
f(2)=x(3)-(0.26936*x(2)*(-1))
f(3)=(2.616*x(2))-(x(4)*x(1)^2)
f(4)=(0.316/(x(3)^(1/4)))
endfunction
function j=jacob(x)
j(1,1)=1;j(1,2)=0.8916*x(2)^(-3);j(1,3)=0;j(1,4)=0
j(2,1)=0;j(2,2)=0.26936*x(2)^(-2);j(2,3)=1;j(2,4)=0;
j(3,1)=-2*x(1)*x(4);j(3,2)=2.616;j(3,3)=0;j(3,4)=-1*x(1)^2;
j(4,1)=0;j(4,2)=0;j(4,3)=-2/x(3)/log(10);j(4,4)=(-0.5*x(4)^(-1.5))-(1/x(4)/log(10));
endfunction
x0=[1 1 2000 1];
[x,v,info]=fsolve(x0,F,jacob);
disp(x);
Error:
[x,v,info]=fsolve(x0,F,jacob);
!--error 98
Variable returned by scilab argument function is incorrect.
at line 17 of exec file called by :
exec('D:\Desktop files\Ajith\TBC\SCILAB code\Chapter_08\fsolve.sce', -1)
Details of the question:-
Actual question: Heated air at 1 atm and 35 degree C is to be transported in a 150m long circular plastic duct at a rate of 0.35 m3/s. If the head loss in the pipe is not to exceed 20m, determine the minimum diameter of the duct?
Book name: Fluid Mechanics: Fundamentals and Applications by Y.A.Cengel and J.M.Cimbala.
Page and question number: Page no.: 345, EXAMPLE 8-4
ISBN of the book: 0-07-247236-7
Textbook link: https://www.academia.edu/32439502/Cengel_fluid_mechanics_6_edition.PDF
In my code: x(1) is velocity, x(2) is the diameter, x(3) is the Reynolds number, x(4) is the friction factor
Expected answers: x(1)=6.24, x(2)=0.267, x(3)=100800, x(4)=0.0180.
My thoughts about the error:
What is see is that if I change the power of the variable such as from 0.5 to 2 or -1.5 to 1, answer is calculated and displayed. So, the problem is somewhere around the power of the variables used.
Also the initial values of the x, I saw that for some initial value there is no error and I got the output.
After reading the description of the problem in the book, there is only one non-trivial equation (the third) all other give directly other unknowns as functions of D. Here is the code to determine the diameter:
function out=F(D)
V = 0.35/%pi/D^2*4;
Re = V*D/1.655e-5;
f = 20/(150/D*V^2/2/9.81);
out = 1/sqrt(f) + 2*log10(2.51/Re/sqrt(f));
endfunction
D0 = 1;
[D,v,info]=fsolve(D0,F);
disp(D)

chen's chaotic system solution using differential transform method

I am calculating the solution of Chen's chaotic system using differential transform method. The code that I am using is:
x=zeros(1,7);
x(1)=-0.1;
y=zeros(1,7);
y(1)=0.5;
z=zeros(1,7);
z(1)=-0.6;
for k=0:5
x(k+2)=(40*gamma(1+k)/gamma(2+k))*(y(k+1)-x(k+1));
sum=0;
for l=0:k
sum=sum+x(l+1)*z(k+1-l);
end
y(k+2)=(gamma(1+k)/gamma(2+k))*(-12*x(k+1)-sum+28*y(k+1));
sum=0;
for l=0:k
sum=sum+x(l+1)*y(k+1-l);
end
z(k+2)=(gamma(1+k)/(1+k))*(sum-3*z(k+1));
end
s=fliplr(x);
t=0:0.05:2;
a=polyval(s,t);
plot(t,a)
What this code does is calculate x(k), y(k) and z(k) these are the coefficients of the polynomial that is approximating the solution.
The solution x(t) = sum_0^infinity x(k)t^k, and similarly the others. But this code doesn't give the desired output of a chaotic sequence the graph of x(t) that I am getting is:
This is not an answer, but a clearer and more correct (programmatically speaking) to write your loop:
for k = 1:6
x(k+1)=(40*1/k)*(y(k)-x(k));
temp_sum = sum(x(1:k).*z(k:-1:1),2);
y(k+1) = (1/k)*(-12*x(k)-temp_sum+28*y(k));
temp_sum = sum(x(1:k).*y(k:-1:1),2);
z(k+1) = (1/k)*(temp_sum-3*z(k));
end
The most important issue here is not overloading the built-in function sum (I replaced it with temp_sum. Other things include vectorization of the inner loops (using sum...), indexing that starts in 1 (instead of writing k+1 all the time), and removing unnecessary calls to gamma (gamma(k)/gamma(k+1) = 1/k).

Calculate the n-th derivative in any point using Scilab

I am trying to evaluate a function in Scilab using the following steps:
x=poly(0,'x')
y=(x^18+x^11)^3 // function (the function is variable)
y1=derivat(y) // first derivate
y2=derivat(y) //second derivate
y3=derivat(y) //third derivate
I need evaluate the 3 derivatives in any point.
I know the function: evstr(expression) but it does not work with the return value of the derivative.
I try to use: string(y) but it returns something strange.
How can to do it, to cast the return of derivat to string to evaluate with evstr or how can I evaluate the n-th derivative in any point using Scilab.
To evaluate numerical derivatives of almost any kind of function (of one or sereval variables) up to machine precision (you won't get better results if you evaluate symbolic expressions obtained by hand), you can use the complex step method (google these terms you will have a bunch of references). For example:
function y = f(x)
s = poly(0,'s');
p = (s-s^2)^3;
y = horner(p,x).*exp(-x.^2);
end
x=linspace(-1,1,100);
d = imag(f(x+complex(0,1e-100)))/1e-100;
true_d = exp(-x.^2).*(-1+x).^2.*x^2.*(3-6*x-2*x.^2+2.*x^3)
disp(max(abs(d-true_d)))
--> disp(max(abs(d-true_d)))
1.776D-15
To evaluate a symbolic polynomial at a particular point or points, use the horner command. Example:
t = 0:0.1:1
v1 = horner(y1, t)
plot(t, v1)
This is the closest I got to a solution to this problem.
He proposes using:
old = 'f';
for i=1:n
new = 'd'+string(i)+'f';
deff('y='+new+'(x)','y=numderivative('+old+',x)');
old=new;
end
I know, it's horrible, but I think there is no better solution, at least in Scilab.
I found a way:
function y = deriva(f, v, n, h)
deff("y = DF0(x)", "y="+f)
if n == 0 then
y = DF0(v);
else
for i=1:(n-1)
deff("y=DF"+string(i)+"(x)", "y=numderivative(DF"+string(i-1)+",x,"+string(h)+",4)");
end
deff("y=DFN(x)", "y=numderivative(DF"+string(n-1)+",x,"+string(h)+",4)");
y = DFN(v);
end
endfunction
disp(deriva("x.*x", 3, 2, 0.0001));
This correctly calculates numerical derivatives of nth order. But it needs to have the function passed as a string. Errors can get pretty large, and time to compute tends to go up fast as a function of n.

Resources