MATLAB Answers

# ODE 45 solution copying initial condition

1 view (last 30 days)
Ji Hwan Park on 13 May 2021
Commented: Ji Hwan Park on 13 May 2021
I am trying to solve second order ODE equation:
Mx'' = -Kx (where M and K are mass and stiffness matrices that are 300 x 300)
X0 = [x0; xdot0]; (each x0 and xdot0 are 300 x 1 vector) (X0: 600 x 1 vector)
f = @(t, x) String(t, x, M, K, N);
function dydt = String(t, X, M, K, N)
x1 = X(1:N);
x2 = X(N+1:end);
dydt1 = x2;
dydt2 = M \ (-K*x1)
% A = -K.*x1;
% A = sum(A, 2);
% dydt2 = M\A;
dydt = [dydt1
dydt2];
end
tSpan = linspace(0 100, 1000)
[~,X] = ode45(f, tSpan, X0);
Issue that I am facing is I am not sure why rows of X are filled with the initial condition X0.
Given function String and using ode45 method, shouldn't it solve the ode and fill the rows with the soltuion accordingly?
Is there any key parts that I am missing? ##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (1)

Steven Lord on 13 May 2021
Rather than handling M yourself in your ODE function consider specifying it as the Mass option using odeset. See this documentation example for how to use a mass matrix.
What happens if you evaluate your ODE function String (which you may want to rename to avoid potential confusion) with the first element of your tspan vector, 0, as the t input and your initial condition vector as the x input? Are all the elements of the output either 0 or very, very small?
##### 1 CommentShowHide None
Ji Hwan Park on 13 May 2021
Evaluating as if like this:
vec = String(0, X0, M, K, N)
Then, it gives me 600 x 1 vector where 301th row has a value of -6.64283192090396 and 600th row has a value of -1.65381707574374.
By the way, Matrix M is a diagonal matrix with mass of string segment as its element, and K is shown below. Sign in to comment.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!