Solving system of ODEs using Octave - octave

I am trying to solve a system of two ODEs using Octave, and in particular the function lsode.
The code is the following:
function xdot = f (x,t)
a1=0.00875;
a2=0.075;
b1=7.5;
b2=2.5;
d1=0.0001;
d2=0.0001;
g=4*10^(-8);
K1=5000;
K2=2500;
n=2;
m=2;
xdot = zeros(2,1);
xdot(1) = a1+b1*x(1)^n/(K1^n+x(1)^n)-g*x(1)*x(2)-d1*x(1);
xdot(2) = a2+b2*x(1)^m/(K2^m+x(1)^m)-d2*x(2);
endfunction
t = linspace(0, 5000, 200)';
x0 = [1000; 1000];
x = lsode ("f", x0, t);
set term dumb;
plot(t,x);
I am getting continuously the same error, that "x" is not defined, and I do not know why. The error is the following:
warning: function name 'f' does not agree with function file name '/home /Simulation 1/sim.m'
error: 'x' undefined near line 17 column 17
error: called from
sim at line 17 column 9
It would we great that any of you could help me with this code.

You have two errors. One, you are not saving your source code with the proper name. Two, variable "x" is a vector, and nothing in your script indicates that. You should add a line "x = zeros(1,2);" right after "xdot = zeros(2,1);".
Try the following code:
function ODEs
t = linspace(0, 5000, 200);
x0 = [1000; 1000];
x = lsode (#f, x0, t);
fprintf('t = %e \t\t x = %e\n',t,x);
endfunction
function xdot = f(x,t)
a1=0.00875;
a2=0.075;
b1=7.5;
b2=2.5;
d1=0.0001;
d2=0.0001;
g=4*10^(-8);
K1=5000;
K2=2500;
n=2;
m=2;
xdot = zeros(2,1);
x = zeros(1,2);
xdot(1) = a1+b1*x(1)^n/(K1^n+x(1)^n)-g*x(1)*x(2)-d1*x(1);
xdot(2) = a2+b2*x(1)^m/(K2^m+x(1)^m)-d2*x(2);
endfunction
Save it as ODEs.m and execute it. It does not plot anything, but gives you an output with the results for the t range you supplied.

Related

Please explain this Octave Error Code: op1 is 1x12, op2 is 1x3

Code:
function result = assess_polynomial_n(y,n)
[coeff, t, pred_accum] = fit_data(y, n);
[avg, std_dev] = compute_error(coeff,y);
[coeff1, t, pred_accum1] = fit_data(y, n+1);
[avg1, std_dev1] = compute_error(coeff1,y);
if avg1>avg
result = n;
elseif avg<0.1 && std_dev<0.1
result = n;
elseif pred_accum1.*(1)<0.0001
result = n;
else
result = 0;
end
end
***Error***
error: compute_error: operator -: nonconformant arguments (op1 is 1x12, op2 is 1x3)
error: called from
compute_error at line 11 column 17
assess_polynomial_n at line 19 column 18
__tester__.octave at line 38 column 1
Does anyone know why I could be getting this error? We're supposed to be working in MATLAB but pasting our code into Moodle which uses Octave if that clears anything up.
I also will post the functions I'm calling for reference since the error is occurring in Compute_Error. This is a part of an overall project where I was to define the functions I'm calling before creating assess_polynomial_n.
function [coeff, t, pred_accum] = fit_data(y, n)
len = length(y);
t = 10*[0:len-1];
coeff = polyfit(t, y, n);
pred_accum = polyval(coeff, t);
end
function [avg std_dev] = compute_error(x,y)
s=[];
for i=1:length(x)
error=abs(x(i)-y(i));
s=[s,error];
end
avg = mean(s);
std_dev = std(s);
end
The problem lies in this line of code:
[avg1, std_dev1] = compute_error(coeff1,y);
You are trying to compute the error between the original data and the coefficients of the fitted polynomial. It doesn't make sense, right?
Instead, you should compute the error between the original data and the fitted data, which is the polynomial's output at each t value. The correct code would be:
[avg1, std_dev1] = compute_error(pred_accum1,y);

Please specify the error in this octave code

So I am trying to solve this problem from coursera regarding onevsAll classification. It comes under the ml course under Andrew NG. I am not able to find mistake in my code and it keeps showing this error
error: fmincg: operator +: nonconformant arguments (op1 is 401x1, op2 is 4x1)
error: called from
fmincg at line 87 column 5
oneVsAll at line 60 column 13
ex3 at line 77 column 13
I have tried reading the fmincg code but I cannot understand the problem.
function [all_theta] = oneVsAll(X, y, num_labels, lambda)
% Some useful variables
m = size(X, 1);
n = size(X, 2);
% You need to return the following variables correctly
all_theta = zeros(num_labels, n + 1);
% Add ones to the X data matrix
X = [ones(m, 1) X];
for c = 1:num_labels
initial_theta = zeros(n+1,1) ;
options = optimset('GradObj', 'on', 'MaxIter', 50);
[theta] = fmincg (#(t)(lrCostFunction(t, X, (y == c), lambda)),initial_theta, options);
all_theta(c, :) = theta;
end;
Program is showing error mentioned above regarding the fmincg file.

Pass external variable to function to sove with lsode in octave

I have a function that I want to solve with lsode, like that:
function f = fcn(x,t)
% Set spring stiffness and mass
k = 500;
m = 5;
% Assign function
f1 = x(2);
f2 = -(k/m)*x(1);
f = [f1; f2];
endfunction
And this is the script that call this function:
% Initial value
x0 = [0.2, 0];
%time
t = linspace(0,1,500);
[X] = lsode (#fcn, x0, t);
plot(t,X(:,1));
Now, I want to loop this script, but every time I want a different value of "k". The function to be solved with lsode must accept only "x" and "t" as argument, then I can't put "k" on the argument-list.
I can't use an anonymous function, because the real function that I'm studying is more complicated (not just a linear oscillator).

Octave - System of differential equations with lsode

here is my problem. I'm trying to solve a system of two differential equations thanks to the two functions below. The part of the code that give me some trouble is the variable "rho". "rho" is a function which values are given from a file and that I tried to put in the the variable rho.
function [xdot]=f2(x,t)
# Parameters of the equations
t=[1:1:35926];
x = dlmread('data_txt.txt');
rho=x(:,4);
beta = 0.68*10^-2;
g = 1.5;
l = 1.6;
# Definition of the system of 2 ODE's :
xdot(1) = ((rho-beta)/g)*x(1) + l*x(2);
xdot(2) = (beta/g)*x(1)-l*x(2);
endfunction
.
# Initial conditions for the two variables :
x0 = [0;1];
# Definition of the time-vector -> (initial time,temporal mesh,final time) :
t = linspace (1, 10, 10000);
# Resolution with the lsode routine :
x = lsode ("f2", x0, t);
# Plot of the two curves :
plot (t,x);
When I run my code, I get the following error:
>> resolution_effective2
error: f2: A(I) = X: X must have the same size as I
error: called from
f2 at line 34 column 9
resolution_effective2 at line 8 column 3
error: lsode: evaluation of user-supplied function failed
error: called from
resolution_effective2 at line 8 column 3
error: lsode: inconsistent sizes for state and derivative vectors
error: called from
resolution_effective2 at line 8 column 3
I know that my error comes from a mismatch of size between some variable but I have been looking for the error for days now and I don't see. Could someone try to give and explain me an effective correction ?
Thank you
The error might come from your function f2. Its first argument x should have the same dimension as x0 since x0 is the initial condition of x.
In your case, whatever you intent to be the first argument of f2 is ignored since in your function you do x = dlmread('data_txt.txt'); this seems to be a mistake.
Then, xdot(1) = ((rho-beta)/g)*x(1) + l*x(2); will be a problem since rho is a vector.
You need to check the dimensions of x and rho.

Matlab second order derivative

I have followed the tutorial on http://www.mit.edu/people/abbe/matlab/ode.html and prepared a function as follows:
function dxy = diffxy(xy)
%
%split xy into variables in our equations
%
x = xy(1);
xdot = xy(2);
y = xy(3);
%
% define the derivatives of these variables from equations
%
xdot = xdot;
ydot = 3*x + 2*y + 5;
xdoubledot = 3 - ydot + 2*xdot;
%
%return the derivatives in dxy in the right order
%
dxy = [xdot; xdoubledot; ydot]
end
When I call it using
[T, XY] = ode45('diffxy',0,10,[0 1 0])
I get an error
??? Error using ==> diffxy
Too many input arguments.
I also tried
XY= ode45(#diffxy,[0 10],[0;1;0])
Anybody have any idea?
haven't read the whole tutorial but aren't you supposed to define your function as
function dxy = diffxy(t, xy)
where t is time vector