I was searching for ways to terminate MATLAB ode when it meets a particular condition. I found the answer in this topic MatLab ODE start/stop conditions, where the use of 'events' was discussed. However this applies to ode45 and when I attempted to use 'events' with ode15i, it simply didn't worked and MATLAB is showing error.
I am trying to learn this with simple example and solved a simple system of differential equations as given below.
dx/dt = 5x + 3y; dy/dt = x + 7y; I solved them using ode45 and tried to do the same with ode15i, but it doesn't work. Given below are my codes.
With ode45
function start_stop_test_ode45
y0 = [5;1];
tv = linspace(0,2,100);
options = odeset('Events',#events);
f = #(t,y) [5*y(1) + 3*y(2);y(1) + 7*y(2)];
[t,Y] = ode45(f,tv,y0,options);
xNI = Y(:,1);
yNI = Y(:,2);
xCF = 3*exp(4*t) + 2*exp(8*t);
yCF = -1*exp(4*t) + 2*exp(8*t);
% Here we plot all the graphs
figure(1)
plot(t,xNI,'--k',t,xCF,'r','Linewidth',1.75)
xlabel('t (s)')
ylabel('x')
legend('Numerical Solution','Closed Form Solution')
figure(2)
plot(t,yNI,'--k',t,yCF,'r','Linewidth',1.75)
xlabel('t (s)')
ylabel('y')
legend('Numerical SOlution','Closed Form Solution')
% Here we solve plot the variation of x with y
figure(3)
plot(xNI,yNI,'k','Linewidth',2);
end
function [value,isterminal,direction] = events(t,y)
value = [y(1) - 7782;y(2) - 8863]; % Detect y = 7356
isterminal = [1;1];
direction = [0;0];
end
With ode15i
function start_stop_test_ode15i
clc;clear all
t0 = 0;
y0 = [5;1];
Fxdy0 = [1;1];
Fxdyp0 = [0;0];
yp0 = [28;12];
tRange = [0 2];
options = odeset('Events',#events);
[y0,yp0] = decic(#ode15ifun,t0,y0,Fxdy0,yp0,Fxdyp0);
sol = ode15i(#ode15ifun,tRange,y0,yp0,options);
tv = linspace(0,2,100);
sv = deval(sol,tv);
sv = sv';
t = tv;
xNI = sv(:,1);
yNI = sv(:,2);
xCF = 3*exp(4*t) + 2*exp(8*t);
yCF = -1*exp(4*t) + 2*exp(8*t);
% Here we plot all the graphs
figure(4)
plot(t,xNI,'--k',t,xCF,'r','Linewidth',1.75)
xlabel('t (s)')
ylabel('x')
legend('Numerical Solution','Closed Form Solution')
figure(5)
plot(t,yNI,'--k',t,yCF,'r','Linewidth',1.75)
xlabel('t (s)')
ylabel('y')
legend('Numerical SOlution','Closed Form Solution')
% Here we solve plot the variation of x with y
figure(6)
plot(xNI,yNI,'k','Linewidth',2);
end
function [value,isterminal,direction] = events(t,y)
value = [y(1) - 7782;y(2) - 8863]; % Detect y = 7356
isterminal = [1;1];
direction = [0;0];
end
Where the ode15ifun is
function res = ode15ifun(t,y,yp)
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
res1 = yp(1) - 5*y(1)- 3*y(2);
res2 = yp(2) - y(1) - 7*y(2);
res = [res1;res2];
end
ode45 is working fine but while using ode15i I am getting error message. Can anyone help as to how to do the same with ode15i?
Thank you very much
(In Response to TroyHaskin) I am adding an image file of the error message for your reference.
Related
I am trying to solve the 1D ADE
This is my code so far:
clc; clear; close all
%Input parameters
Ao = 1; %Initial value
L = 0.08; %Column length [m]
nx = 40; %spatial gridpoints
dx = L/nx; %Length step size [m]
T = 20/24; %End time [days]
nt = 100; %temporal gridpoints
dt = T/nt; %Time step size [days]
Vel = dx/dt; %Velocity in each cell [m/day]
alpha = 0.002; %Dispersivity [m]
De = alpha*Vel; % Dispersion coeff. [m2/day]
%Gridblocks
x = 0:dx:L;
t = 0:dt:T;
%Initial and boundary conditions
f = #(x) x; % initial cond.
% boundary conditions
g1 = #(t) Ao;
g2 = #(t) 0;
%Initialization
A = zeros(nx+1, nt+1);
A(:,1) = f(x);
A(1,:) = g1(t);
gamma = dt/(dx^2);
beta = dt/dx;
% Implementation of the explicit method
for j= 1:nt-1 % Time Loop
for i= 2:nx-1 % Space Loop
A(i,j+1) = (A(i-1,j))*(Vel*beta + De*gamma)...
+ A(i,j)*(1-2*De*gamma-Vel*beta) + A(i+1,j)*(De*gamma);
end
% Insert boundary conditions for i = 1 and i = N
A(2,j+1) = A(1,j)*(Vel*beta + De*gamma) + A(2,j)*(1-2*De*gamma-Vel*beta) + A(3,j)*(De*gamma);
A(nx,j+1) = A(nx-1,j)*(Vel*beta + 2*De*gamma) + A(nx,j)*(1-2*De*gamma-Vel*beta)+ (2*De*gamma*dx*g2(t));
end
figure
plot(t, A(end,:), 'r*', 'MarkerSize', 2)
title('A Vs time profile (Using FDM)')
xlabel('t'),ylabel('A')
Now, I have been able to solve the problem using MATLAB’s pdepe function (see plot), but I am trying to compare the result with the finite difference method (implemented in the code above). I am however getting negative values of the dependent variable, but I am not sure what exactly I could be doing wrong. I therefore will really appreciate if anyone can help me out here. Many thanks in anticipation.
PS: I can post the code I used for the pdepe if anyone would like to see it.
I am trying to using fminunc to obtain the optimal theta in logistic regression, however I keep getting that:
fminunc stopped because it cannot decrease the objective function
along the current search direction.
Searching online, I found that this is usually the result of a gradient error which I am implementing in logistic_costFunction.m. I re-checked my work but I cannot spot the root cause.
I am not sure how to solve this issue, any help would be appreciated.
Here is my code:
clear all; close all; clc;
%% Plotting data
x1 = linspace(0,3,50);
mqtrue = 5;
cqtrue = 30;
dat1 = mqtrue*x1+5*randn(1,50);
x2 = linspace(7,10,50);
dat2 = mqtrue*x2 + (cqtrue + 5*randn(1,50));
x = [x1 x2]'; % X
subplot(2,2,1);
dat = [dat1 dat2]'; % Y
scatter(x1, dat1); hold on;
scatter(x2, dat2, '*'); hold on;
classdata = (dat>40);
%% Compute Cost and Gradient
% Setup the data matrix appropriately, and add ones for the intercept term
[m, n] = size(x);
% Add intercept term to x and X_test
x = [ones(m, 1) x];
% Initialize fitting parameters
initial_theta = zeros(n + 1, 1);
% Compute and display initial cost and gradient
[cost, grad] = logistic_costFunction(initial_theta, x, dat);
fprintf('Cost at initial theta (zeros): %f\n', cost);
fprintf('Gradient at initial theta (zeros): \n');
fprintf(' %f \n', grad);
%% ============= Part 3: Optimizing using fminunc =============
% In this exercise, you will use a built-in function (fminunc) to find the
% optimal parameters theta.
% Set options for fminunc
options = optimset('GradObj', 'on', 'MaxIter', 400);
% Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[theta, cost] = ...
fminunc(#(t)(logistic_costFunction(t, x, dat)), initial_theta, options);
logistic_costFunction.m
function [J, grad] = logistic_costFunction(theta, X, y)
% Initialize some useful values
m = length(y); % number of training examples
grad = zeros(size(theta));
H = sigmoid(X*theta);
T = y.*log(H) + (1 - y).*log(1 - H);
J = -1/m*sum(T);
% ====================Compute grad==================
for i = 1 : m
grad = grad + (H(i) - y(i)) * X(i,:)';
end
grad = 1/m*grad;
end
sigmoid.m
function g = sigmoid(z)
% Computes thes sigmoid of z
g = zeros(size(z));
g = 1 ./ (1 + (1 ./ exp(z)));
end
I'm new to Matlab and am really struggling even to get to grips with the basics.
I've got a function, myspring, that solves position and velocity of a mass/spring system with damping and a driving force. I can specify values for the spring stiffness (k), damping coefficient (c), and mass (m), in the command window prior to running ode45. What I am unable to do is to define a forcing function (even something simple like g = sin(t)) and use that as the forcing function, rather than having it written into the myspring function.
Can anyone help? Here's my function:
function pdot = myspring(t,p,c,k,m)
w = sqrt(k/m);
g = sin(t); % This is the forcing function
pdot = zeros(size(p));
pdot(1) = p(2);
pdot(2) = g - c*p(2) - (w^2)*p(1);
end
and how I'm using it in the command window:
>> k = 2; c = 2; m = 4;
>> tspan = linspace(0,10,100);
>> x0 = [1 0];
>> [t,x] = ode45(#(t,p)myspring(t,p,c,k,m),tspan,x0);
That works, but what I want should look something like this (I imagine):
function pdot = myspring(t,p,c,k,m,g)
w = sqrt(k/m);
pdot = zeros(size(p));
pdot(1) = p(2);
pdot(2) = g - c*p(2) - (w^2)*p(1);
end
Then
g = sin(t);
[t,x] = ode45(#(t,p)myspring(t,p,c,k,m,g),tspan,x0);
But what I get is this
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in myspring (line 7)
pdot(2) = g - c*p(2) - (w^2)*p(1);
Error in #(t,p)myspring(t,p,c,k,m,g)
Error in odearguments (line 87)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Horchler, thank you for the reply. I can do as you suggested and it works. I am now faced with another problem that I hope you could advise me on.
I have a script that calculates the force on a structure due to wave interaction using Morison's equation. I gave it an arbitrary time span to begin with. I would like to use the force F that script calculates as the input driving force to myspring. Here is my Morison script:
H = 3.69; % Wave height (m)
A = H/2; % Wave amplitude (m)
Tw = 9.87; % Wave period (s)
omega = (2*pi)/Tw; % Angular frequency (rad/s)
lambda = 128.02; % Wavelength (m)
k = (2*pi)/lambda; % Wavenumber (1/m)
dw = 25; % Water depth (m)
Cm = 2; % Mass coefficient (-)
Cd = 0.7; % Drag coefficient (-)
rho = 1025; % Density of water (kg/m^3)
D = 3; % Diameter of structure (m)
x = 0; % Fix the position at x = 0 for simplicity
t = linspace(0,6*pi,n); % Define the vector t with n time steps
eta = A*cos(k*x - omega*t); % Create the surface elevation vector with size equal to t
F_M = zeros(1,n); % Initiate an inertia force vector with same dimension as t
F_D = zeros(1,n); % Initiate a drag force vector with same dimension as t
F = zeros(1,n); % Initiate a total force vector with same dimension as t
fun_inertia = #(z)cosh(k*(z+dw)); % Define the inertia function to be integrated
fun_drag = #(z)(cosh(k*(z+dw)).*abs(cosh(k*(z+dw)))); % Define the drag function to be integrated
for i = 1:n
F_D(i) = abs(((H*pi)/Tw) * (1/sinh(k*dw)) * cos(k*x - omega*t(i))) * ((H*pi)/Tw) * (1/sinh(k*dw)) * cos(k*x - omega*t(i)) * integral(fun_drag,-dw,eta(i));
F_M(i) = (Cm*rho*pi*(D^2)/4) * ((2*H*pi^2)/(Tw^2)) * (1/(sin(k*dw))) * sin(k*x - omega*t(i)) * integral(fun_inertia,-dw,eta(i));
F(i) = F_D(i) + F_M(i);
end
Any further advice would be much appreciated.
You can't pre-calculate your forcing function. It depends on time, which ode45 determines. You need to define g as a function and pass a handle to it into your integration function:
...
g = #(t)sin(t);
[t,x] = ode45(#(t,p)myspring(t,p,c,k,m,g),tspan,x0);
And then call it I n your integration function, passing in the current time:
...
pdot(2) = g(t) - c*p(2) - (w^2)*p(1);
...
I'm trying to simulate the time behavior for a physical process governed by a system of ODEs. When I switch the width of the input pulse from 20 to 19, there is no depletion of the y(1) state, which doesn't make sense physically. What am I doing wrong? Am I using ode45 incorrectly?
function test
width = 20;
center = 100;
tspan = 0:0.1:center+50*(width/2);
[t,y] = ode45(#ODEsystem,tspan,[1 0 0 0]);
plot(t,y(:,1),'k*',t,y(:,2),'k:',t,y(:,3),'k--',t,y(:,4),'k');
hold on;
axis([center-3*(width/2) center+50*(width/2) -0.1 1.1])
xlabel('Time')
ylabel('Relative values')
legend({'y1','y2','y3','y4'});
function dy = ODEsystem(t,y)
k1 = 0.1;
k2 = 0.000333;
k3 = 0.1;
dy = zeros(size(y));
% rectangular pulse
I = rectpuls(t-center,width);
% ODE system
dy(1) = -k1*I*y(1);
dy(2) = k1*I*y(1) - k2*y(2);
dy(3) = k2*y(2) - k3*I*y(3);
dy(4) = k3*I*y(3);
end
end
You are changing the parameters of your your ODEs discontinuously in time. This results in a very stiff system and less accurate, or even completely wrong, results. In this case, because the your ODE is so simple when I = 0, an adaptive solver like ode45 will take very large steps. Thus, there's a high probability that it will step right over the region where you inject the impulse and never see it. See my answer here if you're confused as to why the code in your question misses the pulse even though you've specified tspan to have (output) steps of just 0.1.
In general it is a bad idea to have any discontinuities (if statements, abs, min/max, functions like rectpuls, etc.) in your integration function. Instead, you need to break up the integration and calculate your results piecewise in time. Here's a modified version of your code that implements this:
function test_fixed
width = 19;
center = 100;
t = 0:0.1:center+50*(width/2);
I = rectpuls(t-center,width); % Removed from ODE function, kept if wanted for plotting
% Before pulse
tspan = t(t<=center-width/2);
y0 = [1 0 0 0];
[~,out] = ode45(#(t,y)ODEsystem(t,y,0),tspan,y0); % t pre-calculated, no need to return
y = out;
% Pulse
tspan = t(t>=center-width/2&t<=center+width/2);
y0 = out(end,:); % Initial conditions same as last stage from previous integration
[~,out] = ode45(#(t,y)ODEsystem(t,y,1),tspan,y0);
y = [y;out(2:end,:)]; % Append new data removing identical initial condition
% After pulse
tspan = t(t>=center+width/2);
y0 = out(end,:);
[~,out] = ode45(#(t,y)ODEsystem(t,y,0),tspan,y0);
y = [y;out(2:end,:)];
plot(t,y(:,1),'k*',t,y(:,2),'k:',t,y(:,3),'k--',t,y(:,4),'k');
hold on;
axis([center-3*(width/2) center+50*(width/2) -0.1 1.1])
xlabel('Time')
ylabel('Relative values')
legend({'y1','y2','y3','y4'});
function dy = ODEsystem(t,y,I)
k1 = 0.1;
k2 = 0.000333;
k3 = 0.1;
dy = zeros(size(y));
% ODE system
dy(1) = -k1*I*y(1);
dy(2) = k1*I*y(1) - k2*y(2);
dy(3) = k2*y(2) - k3*I*y(3);
dy(4) = k3*I*y(3);
end
end
See also my answer to a similar question.
% Code slightly modified from Signals and Systems by Lathi MS2P4 pg 232.
How can I make this code a function? So that I can just "call" it to use it. Instead of pasting this whole thing over and over. This is just a convolution animation of two functions.
figure(1) % Create figure window and make visible on screen
x = #(t) cos(2*pi*3*t).* u(t);
h = #(t) u(t)-u(t-1);
dtau = 0.005;
tau = -1:dtau:4;
ti = 0;
tvec = -1:0.1:5;
y = NaN*zeros(1,length(tvec)); % Pre-allocate memory
for t = tvec,
ti = ti + 1; % Time index
xh = x(t-tau).*h(tau);
lxh = length(xh);
y(ti) = sum(xh.*dtau); % Trapezoidal approximation of integral
subplot(2,1,1)
plot(tau,h(tau),'k-',tau,x(t-tau),'k--',t,0,'ok')
axis([tau(1) tau(end) -2.0 2.5])
patch([tau(1:end-1);tau(1:end-1);tau(2:end);tau(2:end)],...
[zeros(1,lxh-1);xh(1:end-1);xh(2:end);zeros(1,lxh-1)],...
[0.8 0.8 0.8],'edgecolor','none')
xlabel('\tau')
legend('h(\tau)','x(t-\tau)','t','h(\tau)x(t-\tau)')
c = get(gca,'children');
set(gca,'children',[c(2);c(3);c(4);c(1)]);
subplot(2,1,2)
plot(tvec,y,'k',tvec(ti),y(ti),'ok')
xlabel('t')
ylabel('y(t) = \int h(\tau)x(t-\tau) d\tau')
axis([tau(1) tau(end) -1.0 2.0])
grid
drawnow
% pause
end
You can use Matlab's function , as described here
So, you have to write on the top "function yourFunctionName" and then you can call it using its name, yourFunctionName in this case.
Here is what worked with your exact code, a straightforward function definition and the addition of u(t) that was missing from the OP:
figure(1) % figure window outside function
u = #(t)heaviside(t); % define step function
x = #(t) cos(2*pi*3*t).* u(t);
h = #(t) u(t)-u(t-1);
dtau = 0.005;
convol_anime(x, h, dtau); % pass function handles + parameters
where you have already defined:
function convol_anime(x, h, dtau)
tau = -1:dtau:4; ti = 0; tvec = -1:0.1:5;
y = nan*zeros(1,length(tvec));
...