Problem in solving ordinary differential equation in scilab - 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) ?

Related

TypeError: can't convert complex to float (Python3, solving for algebraic equations)

I have a simple formula for which I want to solve. Assuming all variables other than x are known, I am trying to solve for x by the following folmula: x = [(c-a)/a]^(1/b)
The initial equation was: a * x^b - a = c, and that was my way for solving for x.
Below is a snippet of my code.
a = 5000
b = 5
c = 562
x = ((c-a)/a)**(1/b)
But for some reason it cannot handle it. Any suggestions?
I think the correct formula is:
x = ((c+a)/a)**(1/b)

Solve equation of mean field theory with 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

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)

Using Scilab to solve a two equations

I defined the following two functions:
function [z]=f(x,y)
z = x + y - 8
endfunction
function [z]=g(x,y)
z = 2*x + y - 8
endfunction
I then wanted to find the roots of the two functions (equations). That is, I want a pair of numbers (a,b)
such that f(a,b) = g(a,b) = 0. So, I found the function fsolve in the documentation of Scilab which I believe will do what I want. So, I ran the following command:
fsolve([0;0],f,g)
and it produced the following error:
Undefined variable: y
at line 2 of function f called by :
fsolve([0;0],f,g)
I do not understand this error and I am hoping that somebody can tell me what I am doing wrong.
Bob
function c=f(xy),x=xy(1);y=xy(2);c=[(x+y-8);(2*x+y-8)];endfunction
fsolve([0;0],f)

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