I want to solve the 1-d stiff ODE equation dc/dt = -d(uG.cg)/dz + d/dz(Γdc/dz) - Sg using ode15s solver but I keep getting the error:
[IDA ERROR] IDASolve
At t = 0 and h = 3.8147e-009, the error test failed repeatedly or with |h| = hmin.
Based on the dt value I enter, like 0.1, 1 or 10s (which is my time step for the time loop) the value of h in the error message changes. Here is the part of my code which solves the equation:
dt = 1; % time step, [s]
Time = 3600; % modeling duration, [s]
tt = 0 : dt : Time;
Nt = length(tt);
for p = 1 : Nt-1
tspan = [(p-1)*dt p*dt];
options = odeset('reltol', 1e-15, 'abstol', 1e-18, 'normcontrol', 'on', 'refine', 5, 'NonNegative',1);
for ig = 1 : Nz
[t,y4] = ode15s(#(t,y4) ((-uG(ig,p)*y4+uG(ig-1,p)*cg_D(ig-1,p))/dh)+((gamma_dw(ig,p)*cg_D(ig-1,p)-(gamma_dw(ig,p)+gamma_up(ig,p))*y4+gamma_up(ig,p)*cg_D(ig+1,p))/dh^2)- Sg_D(ig,p), tspan, cg_D(ig,p), options);
cg_D(ig,p+1) = y4(end);
end
end
other variables rather than y4 are defined in the code and have known values. These values change in each time step (p=1, P=2, ... p=Nt-1), which is why I can't solve the equation once for a tspan = [0 Nt-1] so I solve it for every p and use the result as initial values for the next time step. ig is the node index. At the end I want to draw a figure of cg_D-t that shows me the change of cg over time.
Related
function Y6
solinit = bvpinit(linspace(0,1),[0;3;1;1],2);
sol = bvp4c(#ode, #bc, solinit);
y = sol.y;
time = sol.parameters*sol.x;
ut = -y(4,:);
figure(1);
plot(time,y([1 2],:)','-'); hold on;
plot(time, ut, 'k:');
axis([0 time(1,end) -1.5 3]);
text(1.3,2.5,'x_1(t)');
text(1.3,.9,'x_2(t)');
text(1.3,-.5,'u(t)');
xlabel('time');
ylabel('states');
title('Numerical solution');
hold off;
% -------------------------------------------------------------------------
% ODE's of augmented states
function dydt = ode(t,y,T)
dydt = T*[2*y(2);4*y(4);0;-2*y(3)];
% -------------------------------------------------------------------------
% boundary conditions: x1(0)=11;p2(0)=2; x2(tf)=3; 3*p1(tf)+p2(2)^2=0
function res = bc(ya,yb,T)
res = [ ya(1) - 11; ya(4); yb(2) - 3; 3*yb(3)+yb(4)^2];
I don't know why The boundary condition function BCFUN should return a column vector of length 5 error message is came.
Can you explain to me please? Thank you so much
The unknown parameters or constants, here T, also count as components. Thus your state has overall 4+1=5 components, requiring the setting of 5 boundary conditions.
Sometimes that is obvious, for instance in a Sturm-Liouville eigenvalue computation with the eigenvalue as parameter you want to avoid the zero solution, so demand that, e.g., the square integral has value 1.
In a ballistic shot with the flight time as parameter where you fix the location of the canon and the target as obvious boundary conditions it is not that obvious what to select as additional BC, but possibilities are to fix the initial speed or the initial angle.
I might be rusty, but I get from the variation of
L = 3*(x1(T)-9)^2 + integral(0,T, 2*u^2 +p^T*(F(x,u)-x') )
the saddle point conditions
T : 0 = 12*x2(T)*(x1(T)-9) + 2*u(T)^2 (BC5)
x(t) : 0 = p'(t)+F_x(x,u)^T*p
p1'(t) = -2*p2(t) (DE3)
p2'(t) = 0 (DE4)
p(t) : x'(t) = F(x,u)
x1'(t) = 2*x2(t) (DE1)
x2'(t) = 4*u(t) (DE2)
u(t) : 0 = 4*u + p^T*F_u(x,u)
u(t) = -p2(t) (OPT)
x1(T): -p1(T)+6*(x1(T)-9) (BC3)
x2(0): 0 = p2(0) (BC4)
This can be directly evaluated as p2=-u=0 (BC4+DE4+OPT) are constant, likewise p1 = 6*(x1(T)-9) (BC3+DE3). This in turn forces x1(T)=9 (BC5) As now x2(t)=3 is constant, we get x1(t)=11+6*t. Then the remaining expression of the functional 3*(2+6*T)^2 is minimal for T=-1/3. For T=0 the minimal value of the functional is L=12.
For me, it seems like the estimated hstep takes quite a long time and long iteration to converge.
I tried it with this first ODE.
Basically, you perform the difference between RK4 with stepsize of h with h/2.Please note that to reach the same timestep value, you will have to use the y value after two timestep of h/2 so that it reaches h also.
frhs=#(x,y) x.^2*y;
Is my code correct?
clear all;close all;clc
c=[]; i=1; U_saved=[]; y_array=[]; y_array_alt=[];
y_arr=1; y_arr_2=1;
frhs=#(x,y) 20*cos(x);
tol=0.001;
y_ini= 1;
y_ini_2= 1;
c=abs(y_ini-y_ini_2)
hc=1
all_y_values=[];
for m=1:500
if (c>tol || m==1)
fprintf('More')
y_arr
[Unew]=vpa(Runge_Kutta(0,y_arr,frhs,hc))
if (m>1)
y_array(m)=vpa(Unew);
y_array=y_array(logical(y_array));
end
[Unew_alt]=Runge_Kutta(0,y_arr_2,frhs,hc/2);
[Unew_alt]=vpa(Runge_Kutta(hc/2,Unew_alt,frhs,hc/2))
if (m>1)
y_array_alt(m)=vpa(Unew_alt);
y_array_alt=y_array_alt(logical(y_array_alt));
end
fprintf('More')
%y_array_alt(m)=vpa(Unew_alt);
c=vpa(abs(Unew_alt-Unew) )
hc=abs(tol/c)^0.25*hc
if (c<tol)
fprintf('Less')
y_arr=vpa(y_array(end) )
y_arr_2=vpa(y_array_alt(end) )
[Unew]=Runge_Kutta(0,y_arr,frhs,hc)
all_y_values(m)=Unew;
[Unew_alt]=Runge_Kutta(0,y_arr_2,frhs,hc/2);
[Unew_alt]=Runge_Kutta(hc/2,Unew_alt,frhs,hc/2)
c=vpa( abs(Unew_alt-Unew) )
hc=abs(tol/c)^0.2*hc
end
end
end
all_y_values
A better structure for the time loop has only one place where the time step is computed.
x_array = [x0]; y_array = [y0]; h = h_init;
x = x0; y = y0;
while x < x_end
[y_new, err] = RK4_step_w_error(x,y,rhs,h);
factor = abs(tol/err)^0.2;
if factor >= 1
y_array(end+1) = y = y_new;
x_array(end+1) = x = x+h;
end
h = factor*h;
end
For the data given in the code
rhs = #(x,y) 20*cos(x);
x0 = 0; y0 = 1; x_end = 6.5; tol = 1e-3; h_init = 1;
this gives the result against the exact solution
The computed points lie exactly on the exact solution, for the segments between them one would need to use a "dense output" interpolation. Or as a first improvement, just include the middle value from the half-step computation.
function [ y_next, err] = RK4_step_w_error(x,y,rhs,h)
y2 = RK4_step(x,y,rhs,h);
y1 = RK4_step(x,y,rhs,h/2);
y1 = RK4_step(x+h/2,y1,rhs,h/2);
y_next = y1;
err = (y2-y1)/15;
end
function y_next = RK4_step(x,y,rhs,h)
k1 = h*rhs(x,y);
k2 = h*rhs(x+h/2,y+k1);
k3 = h*rhs(x+h/2,y+k2);
k4 = h*rhs(x+h,y+k3);
y_next = y + (k1+2*k2+2*k3+k4)/6;
end
Revision 1
The error returned is the actual step error. The error that is required for the step size control however is the unit step error or error density, which is the step error with divided by h
function [ y_next, err] = RK4_step_w_error(x,y,rhs,h)
y2 = RK4_step(x,y,rhs,h);
y1 = RK4_step(x,y,rhs,h/2);
y1 = RK4_step(x+h/2,y1,rhs,h/2);
y_next = y1;
err = (y2-y1)/15/h;
end
Changing the example to a simple bi-stable model oscillating between two branches of stable equilibria
rhs = #(x,y) 3*y-y^3 + 3*cos(x);
x0 = 0; y0 = 1; x_end = 13.5; tol = 5e-3; h_init = 5e-2;
gives plots of solution, error (against an ode45 integration) and step sizes
Red crosses are the step sizes of rejected steps.
Revision 2
The error in the function values can be used as an error guidance for the extrapolation value which is of 5th order, making the method a 5th order method in extrapolation mode. As it uses the 4th order error to predict the 5th order optimal step size, a caution factor is recommended, the code changes in the appropriate places to
factor = 0.75*abs(tol/err)^0.2;
...
function [ y_next, err] = RK4_step_w_error(x,y,rhs,h)
y2 = RK4_step(x,y,rhs,h);
y1 = RK4_step(x,y,rhs,h/2);
y1 = RK4_step(x+h/2,y1,rhs,h/2);
y_next = y1+(y1-y2)/15;
err = (y1-y2)/15;
end
In the plots the step size is appropriately larger, but the error shows sharper and larger spikes, this version of the method is apparently less stable.
I'm using ode15s to simulate/solve a set of ODEs. I would like to implement a feature, where upon reaching a given condition during the simulation, a number in the model changes programatically (e.g., an indicator constant) for a fixed amount of time, and then reverts back.
This could be, for example using Lotka-Volterra equations:
dx/dt = alphax - betax*y
dy/dt = (delta+indicator)xy - gammay + epsilonindicator
indicator starts as 0. Let's say that when x reaches 10, I'd like to switch indicator to 1 for 10 time units, and then flip it back to 0.
This can be done in a dirty way by using global variables, however, this is something I'd like to avoid (impossible to parallelize + general avoidance of global variables). Is there a neat alternative way when using ode15s (i.e., I don't know the time step)?
Many thanks for any suggestions!
Edit: As noted correctly by LutzL, wrapping an ODE with non-smooth state without handling events may lead to inaccurate results
as you can not predict at what time points in what order the ODE
function is evaluated. LutzL
So the accurate solution is to deal with ODE events. An example for the modified Lotka-Volterra equations is given below, where the event fires, if x gets >10 and the indicator will be turned on for 10 seconds:
% parameters and times:
params = ones(5,1); % [alpha, ..., epsilon]
z_start = [2, 1];
t_start = 0;
t_end = 30;
options = odeset('Events',#LotkaVolterraModEvents); % set further options here, too.
% wrap ODE function with indicator on and off
LVModODE_indicatorOn = #(t,z)LotkaVolterraModODE(t,z,1, params);
LVModODE_indicatorOff = #(t,z)LotkaVolterraModODE(t,z,0, params);
% storage for simulation values:
data.t = t_start;
data.z = z_start;
data.teout = [];
data.zeout = zeros(0,2);
data.ieout = [];
% temporary loop variables:
z_0 = z_start;
t_0 = t_start;
isIndicatorActive = false;
while data.t(end) < t_end % until the end time is reached
if isIndicatorActive
% integrate for 10 seconds, if indicator is active
active_seconds = 10;
[t, z, te,ze,ie] = ode15s(LVModODE_indicatorOn, [t_0 t_0+active_seconds], z_0, options);
else
% integrate until end or event, if indicator is not active.
[t, z, te,ze,ie] = ode15s(LVModODE_indicatorOff, [t_0 t_end], z_0, options);
isIndicatorActive = true;
end
%append data to storage
t_len = length(t);
data.t = [data.t; t(2:end)];
data.z = [data.z; z(2:end,:)];
data.teout = [data.teout; te];
data.zeout = [data.zeout; ze];
data.ieout = [data.ieout; ie];
% reinitialize start values for next iteration of loop
t_0 = t(end);
z_0 = z(end, :);
% set the length of the last instegration
options = odeset(options,'InitialStep',t(end) - t(end-1));
end
%% plot your results:
figure;
plot(data.t, data.z(:,1), data.t, data.z(:,2));
hold all
plot(data.teout, data.zeout(:,1), 'ok');
legend('x','y', 'Events in x')
%% Function definitions for integration and events:
function z_dot = LotkaVolterraModODE(t, z, indicator, params)
x = z(1); y= z(2);
% state equations: modified Lotka-Volterra system
z_dot = [params(1)*x - params(2)*y;
(params(4) + indicator)*x*y - params(3)*y + params(5)*indicator];
end
function [value, isTerminal, direction] = LotkaVolterraModEvents(t,z)
x = z(1);
value = x-10; % event on rising edge when x passes 10
isTerminal = 1; %stop integration -> has to be reinitialized from outer logic
direction = 1; % only event on rising edge (i.e. x(t_e-)<10 and x(t_e+)>10)
end
The main work is done in the while loop, where the integration takes place.
(Old post) The following solution may lead to inaccurate results, handling events, as explained in the first part should be preferred.
You could wrap your problem in a class, which is able to hold a state (i.e. its properties). The class should have a method, which is used as odefun for the variable-step integrator. See also here on how to write classes in MATLAB.
The example below demonstrates, how it could be achieved for the example you provided:
% file: MyLotkaVolterra.m
classdef MyLotkaVolterra < handle
properties(SetAccess=private)
%define, if the modified equation is active
indicator;
% define the start time, where the condition turned active.
tStart;
% ode parameters [alpha, ..., epsilon]
params;
end
methods
function self = MyLotkaVolterra(alpha, beta, gamma, delta, epsilon)
self.indicator = 0;
self.tStart = 0;
self.params = [alpha, beta, gamma, delta, epsilon];
end
% ODE funciton for the state z = [x;y] and time t
function z_dot = odefun(self, t, z)
x = z(1); y= z(2);
if (x>=10 && ~self.indicator)
self.indicator = 1;
self.tStart = t;
end
%condition to turn indicator off:
if (self.indicator && t - self.tStart >= 10)
self.indicator = false;
end
% state equations: modified Lotka-Volterra system
z_dot = [self.params(1)*x - self.params(2)*y;
(self.params(4) + self.indicator)*x*y - ...
self.params(3)*y + self.params(5)*self.indicator];
end
end
end
This class could be used as follows:
% your ode using code:
% 1. create an object (`lvObj`) from the class with parameters alpha = ... = epsilon = 1
lvObj = MyLotkaVolterra(1, 1, 1, 1, 1);
% 2. pass its `odefun`method to the integrator (exaple call with ode15s)
[t,y] = ode15s(#lvObj.odefun, [0,5], [9;1]); % 5 seconds
I am trying to find a solution for a non-linear system in MATLAB using the fsolve function. I have an idea of the region the solution is, so my initial points are a random variation inside this area. The code follows:
%MATLAB parameters
digits = 32;
format shortEng
%Initialization of solving process
min_norm = realmax;
options = optimoptions('fsolve', 'Algorithm', 'trust-region-dogleg',...
'Display', 'off', 'FunValCheck', 'on', 'Jacobian', 'on',...
'DerivativeCheck', 'on');
%Solving loop
while 1
%Refreshes seed after 100000 iterations
rng('shuffle');
for n=1:100000
%Initial points are randomly placed in solving region
x_0 = zeros(1, 3);
x_0(1) = 20*(10^-3)+ abs(3*(10^-3)*randn);
x_0(2) = abs(10^(-90) + 10^-89*randn);
x_0(3) = abs(10*randn);
%Solving
[x, fval] = fsolve(#fbnd, x_0, options);
norm_fval = norm(fval);
%If norm is minimum, result is displayed
if all(x > 0.0) && (norm_fval < min_norm)
iter_solu = x;
display(norm_fval);
display(iter_solu);
min_norm = norm_fval;
end
end
end
The function to be optimized and its jacobian follow:
function [F, J] = fbnd(x)
F(1) = x(1) - x(2)*(exp(7.56/(1.5*0.025))-1);
F(2) = x(1) - x(2)*(exp((x(3)*0.02)/(1.5*0.025))-1)-0.02;
F(3) = x(1) - x(2)*(exp((6.06+x(3)*0.018)/(1.5*0.025))-1)-0.018;
J = [[1.0, -3.5790482371355382991651020082214*10^87, 0.0];...
[1.0, 1.0-exp((8/15)*x(3)),-(8/15)*x(2)*exp((8/15)*x(3))];...
[1.0, 1.0-exp(0.48*x(3)+161.6), -0.48*x(2)*exp(0.48*x(3)+161.6)]];
end
When I turn DerivativeCheck on, I get the following error:
Objective function derivatives:
Maximum relative difference between user-supplied
and finite-difference derivatives = 1.
User-supplied derivative element (1,1): 1
Finite-difference derivative element (1,1): 0
For some reason, it things dF(1)/dx(1) should be zero instead of 1, when it is obvious it is 1. So I went ahead and replaced the J(1,1) by 0, and now I get the same problem with J(3,1):
Objective function derivatives:
Maximum relative difference between user-supplied
and finite-difference derivatives = 1.
User-supplied derivative element (3,1): 1
Finite-difference derivative element (3,1): 0
So, I replaced J(3,1) by 0 and it catches only minor precision errors. Is there an explanation why it thinks J(1,1) and J(3,1) should be zero? It seems really strange to me.
I am using the following code to produce a numerical solution to a system of ODE's with 6 boundary conditions.
I am using the initial conditions to obtain a solution but I must vary three other conditions in order to find the true solution. The function I have is as follows:
function diff = prob5diff(M,Fx,Fy)
u0 = [pi/2 0 0]';
sSpan = [0 13];
p = #(t,u) prob5(t,u,M,Fx,Fy);
options = odeset('reltol',1e-6,'abstol',1e-6);
[s,u] = ode45(p,sSpan,u0,options);
L = length(s);
x = u(:,2); y = u(:,3); theta = u(:,1);
diff(1) = x(L) - 5;
diff(2) = y(L);
diff(3) = theta(L) + pi/2;
end
Ultimately, different values of M,Fx, and Fy will produce different solutions and I would like a solution such that the values in diff are as close to zero as possible so I want fsolve to iterate through different values of M,Fx, and Fy
I am receiving the following error: when I call it in this way:
opt = optimset('Display','iter','TolFun',1e-6);
guess = [1;1;1];
soln = fsolve(#prob5diff,guess,opt);
Error in line:
soln = fsolve(#prob5diff,guess,opt);
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
Thanks!
One problem is that you have to call fsolve on prob5diff which takes a single vector input since your guess is a single vector:
prob5diff(x)
M = x(1);
Fx = x(2);
Fy = x(3);