Trouble fitting system of differential equations to data using lsqcurvefit - matlab

In matlab I am trying to fit the duffing equation as a system of differential equations to some generated data using lsqcurvefit. However when i run the code below I get the error:
Error using lsqcurvefit (line 262)
Function value and YDATA sizes are not equal.
Error in de_test (line 10)
[theta,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]= lsqcurvefit(#solve_duffing, theta0,t, data, zdata);
Does anyone have any idea how to fix this?
t = [0 1000];
z0 = [100; 1];
theta0=[1;1;1;1;1;1];
xdata = [3.6 7.7 9.3 4.1 8.6 2.8 1.3 7.9 10.0 5.4];
ydata = [16.5 150.6 263.1 24.7 208.5 9.9 2.7 163.9 325.0 54.3];
zdata = [95.09 23.11 60.63 48.59 89.12 76.97 45.68 1.84 82.17 44.47];
data = [xdata,ydata];
[theta,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]= lsqcurvefit(#solve_duffing, theta0,t, data, zdata);
function [zs, ts] = solve_duffing(theta, t)
% Solve the system using the generic ode45 solver.
% x is the output grid of output time steps
% y is the value of z and z' at each point on the output grid.
% (These labels are assigned by Matlab)
z0 = [100; 1];
[x, y] = ode45(#duffing, t, z0);
ts = x;
% Derive z'' from the results
z = y(:,1);
zt = y(:,2);
ztt = -(theta(1)*zt + theta(2)*(zt.^2) + theta(3)*(zt.^3) ...
+ theta(4) *z + theta(5)*(z.^2) + theta(6)*(z.^3));
zs = [z zt ztt];
% Definition of the Duffing Equation as an system of implicit first
% order linear D.Es.
%
% Inputs
% t - The current time step. As the D.E. is implicit this is ignored.
% However it is required for the numeric solver.
% z - A vector of the values (z, z') for the current time step
% Outputs
% dz - A vector of the derivatives of z (ie. z', z'') for the current
% time step
function dz = duffing(~,z)
dz1 = z(2);
dz2 = -(theta(1)*z(2) + theta(2)*z(2)^2 + theta(3)*z(2)^3 ...
+ theta(4)*z(1) + theta(5)*z(1)^2 + theta(6)*z(1)^3);
dz = [dz1; dz2];
end
end

Related

How do I fit a nonlinear function correctly in matlab

I have the following table, named "test":
0.0037071 0.5
0.015203 1
0.035039 1.5
0.062272 2
0.093988 2.5
0.12776 3
0.16291 3.5
0.19991 4
0.24002 4.5
0.28574 5
0.34696 5.5
0.47879 6
1.8882 6.1125
Now I want do fit a nonlinear function using matlab:
modelfun = #(b,x)erf(b(1)*x)./b(2) + b(3);
beta0 = [0, 0, 0];
mdl = fitnlm(test,modelfun,beta0)
But I get the following error:
Error using nlinfit (line 247)
No usable observations after removing NaNs in Y and in the result of evaluating MODELFUN at the initial value BETA0.
How can I solve this ?
(and how can I get the final fitted nonlinear function for plotting ? )
I am not familiar with fitnlm, but you can use another function from the optimization toolbox, e.g. lsqnonlin.
% splitting your data in vectors
x = data(:,1);
y = data(:,2);
% the model you want to fit
modelfun = #(b,x) erf(b(1)*x)./b(2) + b(3);
% define a cost function, the error between the data to fit and the
% prediction of the model
cost_fun = #(b,x,y) modelfun(b,x) - y;
% initial guess
beta0 = [1 1 1];
% perform optimization
p = lsqnonlin(#(p) cost_fun(p,x,y), beta0);
Turns out that your model returns NaN values for beta0 all zeros. This is why you get the error. This is caused by the fact that you are dividing by 0 initially, b(2) = 0. Changing your beta0 to ones, solves the issue, and you can use fitnlm:
% splitting your data in vectors
x = data(:,1);
y = data(:,2);
modelfun = #(b,x) erf(b(1)*x)./b(2) + b(3);
% initial guess
beta0 = [1 1 1];
mdl = fitnlm(x,y,modelfun,beta0)
To plot the data, just extract the parameters from the table in mdl and store them in b_est (first column), and do y_est = modelfun(b_est,x). In case you use lsqnonlin they are stored in the output variable p
Then just plot: plot(t,y,t,y_est)

MATLAB - passing a sinusoidal forcing function to ode45

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);
...

Simulink simulation behaving massively different than the same simulation using rk4

I have difficulties simulating an object discribed by the following state space equations in simulink:
The right hand side of the state space equation is described by the funcion below.
function dxdt = RHS( t, x, F)
% parameters
b = 1.22; % cart friction coeffitient
c = 0.0027; %pendulum friction coeffitient
g = 9.81; % gravity
M = 0.548+0.022*2; % cart weight
m = 0.031*2; %pendulum masses
I = 0.046;%0.02*0.025/12+0.02*0.12^2+0.011*0.42^2; % moment of inertia
l = 0.1313;
% x(1) = theta
% x(2) = theta_dot
% x(3) = x
% x(4) = x_dot
dxdt = [x(2);
(-(M+m)*c*x(2)-(M+m)*g*l*sin(x(1))-m^2*l^2*x(2)^2*sin(x(1))*cos(x(1))+m*l*b*x(4)*cos(x(1))-m*l*cos(x(1))*F)/(I*(m+M)+m*M*l^2+m^2*l^2*sin(x(1))^2);
x(4);
(F - b*x(4) + l*m*x(2)^2*sin(x(1)) + (l*m*cos(x(1))*(c*x(2)*(M + m) + g*l*sin(x(1))*(M + m) + F*l*m*cos(x(1)) + l^2*m^2*x(2)^2*cos(x(1))*sin(x(1)) - b*l*m*x(4)*cos(x(1))))/(I*(M + m) + l^2*m^2*sin(x(1))^2 + M*l^2*m))/(M + m)];
end
The coresponding rk4 function with a simple visualisation is shown below.
function [wi, ti] = rk4 ( RHS, t0, x0, tf, N )
%RK4 approximate the solution of the initial value problem
%
% x'(t) = RHS( t, x ), x(t0) = x0
%
% using the classical fourth-order Runge-Kutta method - this
% routine will work for a system of first-order equations as
% well as for a single equation
%
% calling sequences:
% [wi, ti] = rk4 ( RHS, t0, x0, tf, N )
% rk4 ( RHS, t0, x0, tf, N )
%
% inputs:
% RHS string containing name of m-file defining the
% right-hand side of the differential equation; the
% m-file must take two inputs - first, the value of
% the independent variable; second, the value of the
% dependent variable
% t0 initial value of the independent variable
% x0 initial value of the dependent variable(s)
% if solving a system of equations, this should be a
% row vector containing all initial values
% tf final value of the independent variable
% N number of uniformly sized time steps to be taken to
% advance the solution from t = t0 to t = tf
%
% output:
% wi vector / matrix containing values of the approximate
% solution to the differential equation
% ti vector containing the values of the independent
% variable at which an approximate solution has been
% obtained
%
% x(1) = theta
% x(2) = theta_dot
% x(3) = x
% x(4) = x_dot
t0 = 0; tf = 5; x0 = [pi/2; 0; 0; 0]; N = 400;
neqn = length ( x0 );
ti = linspace ( t0, tf, N+1 );
wi = [ zeros( neqn, N+1 ) ];
wi(1:neqn, 1) = x0';
h = ( tf - t0 ) / N;
% force
u = 0.0;
%init visualisation
h_cart = plot(NaN, NaN, 'Marker', 'square', 'color', 'red', 'LineWidth', 6);
hold on
h_pend = plot(NaN, NaN, 'bo', 'LineWidth', 3);
axis([-5 5 -5 5]);
axis manual;
xlim([-5 5]);
ylim([-5 5]);
for i = 1:N
k1 = h * feval ( 'RHS', t0, x0, u );
k2 = h * feval ( 'RHS', t0 + (h/2), x0 + (k1/2), u);
k3 = h * feval ( 'RHS', t0 + h/2, x0 + k2/2, u);
k4 = h * feval ( 'RHS', t0 + h, x0 + k3, u);
x0 = x0 + ( k1 + 2*k2 + 2*k3 + k4 ) / 6;
t0 = t0 + h;
% model output
wi(1:neqn,i+1) = x0';
% model visualisation
%plotting cart
l = 2;
set(h_cart, 'XData', x0(3), 'YData', 0, 'LineWidth', 5);
%plotting pendulum
%hold on;
set(h_pend, 'XData', sin(x0(1))*l+x0(3), 'YData', -cos(x0(1))*l, 'LineWidth', 2);
%hold off;
% regulator
pause(0.02);
end;
figure;
plot(ti, wi);
legend('theta', 'theta`', 'x', 'x`');
This gives realistic looking results for a pendulum on a cart.
Now to the problem.
I wanted to recreate the exact same equations in simulink. I thought it is going to be as easy as creating the following simulink model.
where I fill the fcn blocks with the second and fourth equation from the RHS file. Like this.
(-(M+m)*c*u(2)-(M+m)*g*l*sin(u(1))-m^2*l^2*u(2)^2*sin(u(1))*cos(u(1))+m*l*b*u(3)*cos(u(1))-m*l*cos(u(1))*u(4))/(I*(m+M)+m*M*l^2+m^2*l^2*sin(u(1))^2)
(u(5) - b*u(4) + l*m*u(2)^2*sin(u(1)) + (l*m*cos(u(1))*(c*u(2)*(M + m) + g*l*sin(u(1))*(M + m) + u(5)*l*m*cos(u(1)) + l^2*m^2*u(2)^2*cos(u(1))*sin(u(1)) - b*l*m*u(4)*cos(u(1))))/(I*(M + m) + l^2*m^2*sin(u(1))^2 + M*l^2*m))/(M + m)
The problem is this doesn't give the correct results from above, but the one below
Does anybody know what I do incorrectly?
Edit:After #am304 comment I decided to add the following information. I changed the setting for the simulink solver to use the fixed-step rk4 solver, so as to get the same results. The second integrator3 from the model above has been initialized to pi/2.
Edit2: If somebody wants to check out the simulink model for themselves click on the link to download the file.
Edit3: As you can read in the answer below the problem was trivial. You can download the correct model here
I looked through your Simulink model and it seems you may have mixed up the two functions you were using. You used the theta_dd function where you meant to put x_dd and vice versa.
In your model, you also force x_d to be set to a constant value 0. I assume you actually meant to set the initial condition to 0, which you can see is done via the Integrator block. x_d (as an input to f) should be your state vector which is also an output of your integrators. This is just a consequence of what you define x_d to be, the integral of x_dd. This is how RK4 works as well; you use an initial state vector first and then use the predicted state vector to drive the next RK4 step.
The resulting output from the scope (i've outputted your whole state vector here) is as follows and looks like what you expect:
I do not think I should link externally to the simulink file so if you would like a copy of the file you can open a chat and ask for it. Otherwise the picture above should be sufficient enough to help you reproduce the same results.

Implement finite difference method in matlab

I am trying to implement the finite difference method in matlab. I did some calculations and I got that y(i) is a function of y(i-1) and y(i+1), when I know y(1) and y(n+1). However, I don't know how I can implement this so the values of y are updated the right way. I tried using 2 fors, but it's not going to work that way.
EDIT
This is the script and the result isn't right
n = 10;
m = n+1;
h = 1/m;
x = 0:h:1;
y = zeros(m+1,1);
y(1) = 4;
y(m+1) = 6;
s = y;
for i=2:m
y(i) = y(i-1)*(-1+(-2)*h)+h*h*x(i)*exp(2*x(i));
end
for i=m:-1:2
y(i) = (y(i) + (y(i+1)*(2*h-1)))/(3*h*h-2);
end
The equation is:
y''(x) - 4y'(x) + 3y(x) = x * e ^ (2x),
y(0) = 4,
y(1) = 6
Thanks.
Consider the following code. The central differential quotient is discretized.
% Second order diff. equ.
% y'' - 4*y' + 3*y = x*exp(2*x)
% (y(i+1)-2*y(i)+y(i-1))/h^2-4*(y(i+1)-y(i-1))/(2*h) + 3*y(i) = x(i)*exp(2*x(i));
The solution region is specified.
x = (0:0.01:1)'; % Solution region
h = min(diff(x)); % distance
As said in my comment, using this method, all points have to be solved simultaneously. Therefore, above numerical approximation of the equation is transformed in a linear system of euqations.
% System of equations
% Matrix of coefficients
A = zeros(length(x));
A(1,1) = 1; % known solu for first point
A(end,end) = 1; % known solu for last point
% y(i) y'' y
A(2:end-1,2:end-1) = A(2:end-1,2:end-1)+diag(repmat(-2/h^2+3,[length(x)-2 1]));
% y(i-1) y'' -4*y'
A(1:end-1,1:end-1) = A(1:end-1,1:end-1)+diag(repmat(1/h^2+4/(2*h),[length(x)-2 1]),-1);
% y(i+1) y'' -4*y'
A(2:end,2:end) = A(2:end,2:end)+diag(repmat(1/h^2-4/(2*h),[length(x)-2 1]),+1);
With the rhs of the differential equation. Note that the known values are calculated by 1 in the matrix and the actual value in the solution vector.
Y = x.*exp(2*x);
Y(1) = 4; % known solu for first point
Y(end) = 6; % known solu for last point
y = A\Y;
Having an equation to approximate the first order derivative (see above) you can verify the solution. (note, ddx2 is an own function)
f1 = ddx2(x,y); % first derivative (own function)
f2 = ddx2(x,f1); % second derivative (own function)
figure;
plot(x,y);
saveas(gcf,'solu1','png');
figure;
plot(x,f2-4*f1+3*y,x,x.*exp(2*x),'ko');
ylim([0 10]);
legend('lhs','rhs','Location','nw');
saveas(gcf,'solu2','png');
I hope the solution shown below is correct.

Newton algorithm - couln't calculate Hessian

I'm trying to write an implementation of Newton algorithm in Matlab.
When I call up my function using formula:
result = NewtonMethod(x(1).^2 - 2.1*x(1).^4 + (x(1).^6)/3 + x(1)*x(2) - 4*x(2).^2 + 4*x(2).^4, [0 0], 0.1, 10)
I've got an error message:
??? Undefined function or method 'hessian' for input arguments of type 'double'.
Error in ==> NewtonMethod at 13
H = hessian(f, x0);
I've got no idea what is wrong. Maybe someone more familiar with Matlab can help me.
Below it's my code:
function xnext = NewtonMethod(f, x0, eps, maxSteps)
% x0 - starting point (2 – dimensional vector)
% H - matrix of second derivatives (Hessian)
% eps - required tolerance of calculations
% maxSteps - length of step
x = x0;
for n=1:maxSteps
% determine the hessian H at the starting point x0,
H = hessian(f, x0);
% determine the gradient of the goal function gradf at the point x,
gradF = gradient(f, x);
% determine next point
xnext = x - inv(H) * x * gradF;
if abs(xnext - x) < eps
return %found
else
x = xnext; %update
end
end
It's my first contact with Matlab.
Update:
Now I've got an error:
??? Error using ==> mupadmex
Error in MuPAD command: Index exceeds matrix dimensions.
Error in ==> sym.sym>sym.subsref at 1381
B = mupadmex('symobj::subsref',A.s,inds{:});
I typed:
syms x
result = NewtonMethod(x(1).^2 - 2.1*x(1).^4 + (x(1).^6)/3 + x(1)*x(2) - 4*x(2).^2 + 4*x(2).^4, [0 0], 0.1, 10)
x(1).^2 - 2.1*x(1).^4 + (x(1).^6)/3 + x(1)*x(2) - 4*x(2).^2 + 4*x(2).^4
Is reduced to a double before the NewtonMethod function is called, so when your code reaches hessian(f, x0), you're passing it two double arguments, which is not a supported syntax.
Review the notes on properly specifying a symbolic function, and pass that into NewtonMethod.
It's been a long time since I've done numerical optimization, but take a look at the following:
function xn = NewtonMethod(f, x0, eps, maxSteps)
% x0 - starting point (2 – dimensional vector)
% H - matrix of second derivatives (Hessian)
% eps - required tolerance of calculations
% maxSteps - length of step
syms x y
H = hessian(f);
gradF = gradient(f);
xi = x0;
for i=1:maxSteps
% evaluate f at xi
zi = subs(f, [x,y], xi);
% determine the hessian H at the starting point x0,
hi = subs(H, [x,y], xi);
% determine the gradient of the goal function gradf at the point x,
gi = subs(gradF, [x,y], xi);
% determine next point
ss = 0.5; % step size
xn = xi - ss.* (inv(hi) * gi);
% evaluate f at xn
zn = subs(f, [x,y], xn);
% some debugging spam
zd = zn - zi; % the change in the value of the
si = sprintf('[%6.3f, %6.3f]', xi); % function from xi -> xn
sn = sprintf('[%6.3f, %6.3f]', xn);
printf('Step %3d: %s=%9.4f -> %s=%9.4f : zd=%9.4f\n', i, si, zi, sn, zn, zd);
% stopping condition
if abs(xi - xn) < eps
return %found
else
xi = xn; %update
end
end
And called with
result = NewtonMethod(f, [0; 1], 0.001, 100)