untitled

Pasted Dec 1, 2026, 2:08 PM1 file539 lines

Scan to open on your phone

Point a camera at the QR code - it opens this exact paste, no app needed.

https://codepastes.com/zzgrrhvee17y19y4
untitled
%% ========================================================================
%  TD 3 - Master 2 MMSP
%  Analyse 2D (EF QUA4, contraintes planes) d'une poutre encastrée-libre
%  A. Calcul statique | B. Analyse modale | C. Newmark implicite + Rayleigh
% =========================================================================
clear; close all; clc;

% Ajout automatique du dossier du script et du répertoire courant au Path
script_dir = fileparts(mfilename('fullpath'));
if ~isempty(script_dir)
    addpath(genpath(script_dir));
end
addpath(genpath(pwd));

%% 1. PARAMETRES GEOMETRIQUES ET MECANIQUES (SI)
Lsol = 1.0;        % Longueur de la poutre [m]
Hsol = 0.05;       % Hauteur de la poutre [m]
ep   = 1.0;        % Epaisseur hors-plan [m]

E    = 210e9;      % Module d'Young [Pa] (210 000 MPa)
nu   = 0.3;        % Coefficient de Poisson [-]
rho  = 7800;       % Masse volumique [kg/m^3]
g    = 9.81;       % Gravité [m/s^2]

dof  = 2;          % Degrés de liberté par noeud (UX, UY)

%% ========================================================================
%  PARTIE A : MAILLAGE ET CALCUL STATIQUE
% =========================================================================

%% A.2 Construction du maillage QUA4
scal   = 1;              % Facteur de raffinement (1, 2, 4...)
L_EF_X = 0.05 / scal;    % Taille cible EF selon X [m]
L_EF_Y = 0.01 / scal;    % Taille cible EF selon Y [m]

Lx1 = 0;      Ly1 = -Hsol;   % Coin bas-gauche
Lx2 = Lsol;   Ly2 = 0;       % Coin haut-droite

NSx = round(Lsol / L_EF_X);
NSy = round(Hsol / L_EF_Y);

dx_EF = (Lx2 - Lx1) / NSx;
dy_EF = (Ly2 - Ly1) / NSy;

xp = Lx1 : dx_EF : Lx2;
yp = Ly1 : dy_EF : Ly2;

% Coordonnées des noeuds XA (ligne par ligne, gauche -> droite, bas -> haut)
nnA = (NSx + 1) * (NSy + 1);
XA  = zeros(nnA, 2);
kk  = 1;
for ii = 1:(NSy + 1)
    for jj = 1:(NSx + 1)
        XA(kk, :) = [xp(jj), yp(ii)];
        kk = kk + 1;
    end
end

% Topologie TA : [n1(BG) n2(BD) n3(HD) n4(HG) id_mat]
N_EF = NSx * NSy;
TA   = zeros(N_EF, 5);
ip   = 1;
ll   = 1;
for ii = 1:NSy
    for jj = 0:(NSx - 1)
        TA(ll, :) = [(ip + jj), (ip + jj + 1), (ip + jj + NSx + 2), (ip + jj + NSx + 1), 1];
        ll = ll + 1;
    end
    ip = ip + (NSx + 1);
end

nnodes = size(TA, 2) - 1;   % 4 noeuds par EF
NA     = nnA * dof;         % Nombre total de ddls

% Correspondance ddls locaux -> ddls globaux (ig_mem_SDA) et données patch
ig_mem_SDA = zeros(nnodes * dof, N_EF);
XAdata     = zeros(nnodes, N_EF);
YAdata     = zeros(nnodes, N_EF);
CAdata     = zeros(nnodes, N_EF);

for e = 1:N_EF
    for s = 1:nnodes
        for j = 1:dof
            ig_mem_SDA((s - 1) * dof + j, e) = (TA(e, s) - 1) * dof + j;
        end
    end
    XAdata(:, e) = XA(TA(e, 1:4), 1);
    YAdata(:, e) = XA(TA(e, 1:4), 2);
end

% Visualisation du maillage initial
figure(1); clf;
afficher_maillage(TA, XA, 'r', 1);
axis equal; grid on;
title(sprintf('Maillage initial QUA4 (%d EF, %d noeuds)', N_EF, nnA));

figure(2); clf;
patch(XAdata, YAdata, CAdata, 'EdgeColor', 'k');
grid on; axis equal;
colorbar('South', 'FontSize', 12);
title('Maillage initial (patch)');

%% A.3 Conditions aux limites (encastrement en x = 0)
tol       = 1e-10;
Noeu_BC_x = find(abs(XA(:, 1) - Lx1) < tol);
Noeu_BC_y = find(abs(XA(:, 1) - Lx1) < tol);

ddl_bloques_x     = (Noeu_BC_x - 1) * dof + 1;
ddl_bloques_y     = (Noeu_BC_y - 1) * dof + 2;
liste_ddl_bloques = sort(unique([ddl_bloques_x; ddl_bloques_y]));
liste_ddl_libres  = setdiff(1:NA, liste_ddl_bloques);

% Noeud de suivi en bout de poutre (x = L, fibre neutre y = -Hsol/2)
noeuds_bout  = find(abs(XA(:, 1) - Lx2) < tol);
[~, idx_mid] = min(abs(XA(noeuds_bout, 2) - (-Hsol / 2)));
noeud_bout   = noeuds_bout(idx_mid);

%% A.4 Matrice de comportement H (contraintes planes)
H = (E / (1 - nu^2)) * [ 1,  nu, 0;
                         nu, 1,  0;
                         0,  0, (1 - nu) / 2 ];

%% A.5 Initialisation des matrices globales K et M
KA = sparse(NA, NA);
MA = sparse(NA, NA);

%% A.6 Construction du vecteur des forces externes (poids propre)
disp('Construction des forces externes...');
fext        = zeros(NA, 1);
charge_PP_e = -dx_EF * dy_EF * ep * rho * g;   % Poids d'un EF (selon -Y)

for e = 1:N_EF
    for s = 1:nnodes
        ddl_y = (TA(e, s) - 1) * dof + 2;
        fext(ddl_y) = fext(ddl_y) + charge_PP_e / 4;
    end
end

%% A.7 Calcul et assemblage de la matrice de rigidité globale K (Gauss 2x2)
disp('Assemblage de la matrice de rigidité KA...');
ksi    = [-1,  1,  1, -1] / sqrt(3);
eta    = [-1, -1,  1,  1] / sqrt(3);
wpoids = [ 1,  1,  1,  1];

for e = 1:N_EF
    Xe = XA(TA(e, 1:nnodes), :);
    Ke = zeros(nnodes * dof, nnodes * dof);

    for k = 1:4
        dN_ref = [ -(1 - eta(k)),  (1 - eta(k)), (1 + eta(k)), -(1 + eta(k));
                   -(1 - ksi(k)), -(1 + ksi(k)), (1 + ksi(k)),  (1 - ksi(k)) ] / 4;

        Jt = dN_ref * Xe;
        dN = Jt \ dN_ref;

        B = zeros(3, nnodes * dof);
        for s = 1:nnodes
            B(1, 2*s - 1) = dN(1, s);
            B(2, 2*s)     = dN(2, s);
            B(3, 2*s - 1) = dN(2, s);
            B(3, 2*s)     = dN(1, s);
        end

        Ke = Ke + wpoids(k) * ep * (B' * H * B) * abs(det(Jt));
    end

    ig = ig_mem_SDA(:, e);
    KA(ig, ig) = KA(ig, ig) + Ke;
end

%% A.8 Résolution statique et post-traitement (déformée + sigma_xx)
disp('Résolution statique...');
U_stat = zeros(NA, 1);
U_stat(liste_ddl_libres) = KA(liste_ddl_libres, liste_ddl_libres) \ fext(liste_ddl_libres);

% Comparaison avec la théorie des poutres (Euler-Bernoulli)
I_poutre    = ep * Hsol^3 / 12;
fleche_theo = -rho * g * Hsol * ep * Lsol^4 / (8 * E * I_poutre);
fleche_num  = U_stat((noeud_bout - 1) * dof + 2);
erreur_rel  = 100 * abs(fleche_num - fleche_theo) / abs(fleche_theo);

fprintf('\n--- RESULTATS STATIQUES ---\n');
fprintf('Flèche analytique (Euler-Bernoulli) : %.6e m\n', fleche_theo);
fprintf('Flèche numérique EF (noeud %d)      : %.6e m\n', noeud_bout, fleche_num);
fprintf('Ecart relatif                       : %.2f %%\n', erreur_rel);

% Calcul des contraintes sigma_xx au centre de chaque EF (ksi=0, eta=0)
fact_stat = 0.16 * Lsol / max(abs(U_stat));
XA_def    = XA + fact_stat * [U_stat(1:2:end), U_stat(2:2:end)];

XAdata_def  = zeros(nnodes, N_EF);
YAdata_def  = zeros(nnodes, N_EF);
sigma_xx_el = zeros(nnodes, N_EF);

dN0_ref = [-1, 1, 1, -1; -1, -1, 1, 1] / 4;
for e = 1:N_EF
    Xe  = XA(TA(e, 1:nnodes), :);
    Jt0 = dN0_ref * Xe;
    dN0 = Jt0 \ dN0_ref;

    B0 = zeros(3, nnodes * dof);
    for s = 1:nnodes
        B0(1, 2*s - 1) = dN0(1, s);
        B0(2, 2*s)     = dN0(2, s);
        B0(3, 2*s - 1) = dN0(2, s);
        B0(3, 2*s)     = dN0(1, s);
    end

    ig      = ig_mem_SDA(:, e);
    sigma_e = H * B0 * U_stat(ig);

    XAdata_def(:, e)  = XA_def(TA(e, 1:4), 1);
    YAdata_def(:, e)  = XA_def(TA(e, 1:4), 2);
    sigma_xx_el(:, e) = sigma_e(1) * ones(4, 1);
end

figure(3); clf;
patch(XAdata_def, YAdata_def, sigma_xx_el, 'EdgeColor', 'k');
grid on; axis equal; colorbar;
title(sprintf('Déformée statique (x%.0f) et contraintes \\sigma_{xx} [Pa]', fact_stat));
xlabel('x [m]'); ylabel('y [m]');

%% ========================================================================
%  PARTIE B : ANALYSE MODALE
% =========================================================================

%% B.9 Calcul et assemblage de la matrice de masse consistante MA
disp('Assemblage de la matrice de masse MA...');
for e = 1:N_EF
    Xe = XA(TA(e, 1:nnodes), :);
    Me = zeros(nnodes * dof, nnodes * dof);

    for k = 1:4
        Ne = [ (1 - ksi(k))*(1 - eta(k)), ...
               (1 + ksi(k))*(1 - eta(k)), ...
               (1 + ksi(k))*(1 + eta(k)), ...
               (1 - ksi(k))*(1 + eta(k)) ] / 4;

        dN_ref = [ -(1 - eta(k)),  (1 - eta(k)), (1 + eta(k)), -(1 + eta(k));
                   -(1 - ksi(k)), -(1 + ksi(k)), (1 + ksi(k)),  (1 - ksi(k)) ] / 4;

        Jt = dN_ref * Xe;

        N = [ Ne(1), 0, Ne(2), 0, Ne(3), 0, Ne(4), 0;
              0, Ne(1), 0, Ne(2), 0, Ne(3), 0, Ne(4) ];

        Me = Me + wpoids(k) * ep * rho * (N' * N) * abs(det(Jt));
    end

    ig = ig_mem_SDA(:, e);
    MA(ig, ig) = MA(ig, ig) + Me;
end

% Vérification de la masse totale
masse_X    = full(sum(sum(MA(1:2:end, 1:2:end))));
masse_theo = Hsol * Lsol * ep * rho;
fprintf('\n--- VERIFICATION MASSE ---\n');
fprintf('Masse numérique (selon X) : %.4f kg\n', masse_X);
fprintf('Masse théorique           : %.4f kg\n', masse_theo);

%% B.10 Calcul des modes propres : (K - omega^2 M) phi = 0
N_mode = 5;
KA_lib = KA(liste_ddl_libres, liste_ddl_libres);
MA_lib = MA(liste_ddl_libres, liste_ddl_libres);
KA_lib = 0.5 * (KA_lib + KA_lib');
MA_lib = 0.5 * (MA_lib + MA_lib');

[V_lib, D]     = eigs(KA_lib, MA_lib, N_mode, 'smallestabs');
[lam, idx_ord] = sort(real(diag(D)));
V_lib          = real(V_lib(:, idx_ord));

V = zeros(NA, N_mode);
V(liste_ddl_libres, :) = V_lib;

omega_modes = sqrt(lam);
freq_modes  = omega_modes / (2 * pi);

% Pulsations théoriques (1er mode flexion + 1er mode traction-compression)
omega_theorique          = (1.875104)^2 * sqrt(E * I_poutre / (rho * Hsol * ep * Lsol^4));
omega_theorique_traction = (pi / (2 * Lsol)) * sqrt(E / rho);

fprintf('\n--- ANALYSE MODALE ---\n');
figure(4); clf;
for imode = 1:N_mode
    om_mode = omega_modes(imode);
    f_mode  = freq_modes(imode);
    fprintf('Mode %d : f = %8.2f Hz | omega = %9.2f rad/s | T = %.5f s\n', ...
            imode, f_mode, om_mode, 1 / f_mode);

    u_tot     = V(:, imode) / max(abs(V(:, imode)));
    fact_mode = 0.15 * Lsol;
    XA_new    = XA + fact_mode * [u_tot(1:2:end), u_tot(2:2:end)];

    XAdata_m = zeros(nnodes, N_EF);
    YAdata_m = zeros(nnodes, N_EF);
    CAdata_m = zeros(nnodes, N_EF);
    for e = 1:N_EF
        XAdata_m(:, e) = XA_new(TA(e, 1:4), 1);
        YAdata_m(:, e) = XA_new(TA(e, 1:4), 2);
        CAdata_m(:, e) = u_tot((TA(e, 1:4) - 1) * dof + 2);
    end

    subplot(N_mode, 1, imode);
    patch(XAdata_m, YAdata_m, CAdata_m, 'EdgeColor', 'k');
    hold on;
    afficher_maillage(TA, XA, 'k', 0);
    axis equal; grid on; colorbar;
    title(sprintf('Mode %d : f = %.2f Hz (\\omega = %.2f rad/s)', imode, f_mode, om_mode));
end
fprintf('Mode 1 flexion théorique  : omega = %.2f rad/s (f = %.2f Hz)\n', ...
        omega_theorique, omega_theorique / (2 * pi));
fprintf('Mode 1 traction théorique : omega = %.2f rad/s (f = %.2f Hz)\n', ...
        omega_theorique_traction, omega_theorique_traction / (2 * pi));

%% ========================================================================
%  PARTIE C : INTEGRATION TEMPORELLE - NEWMARK IMPLICITE & RAYLEIGH
% =========================================================================

gamma_N    = 0.5;
beta_N     = 0.25;
dtimp1     = 1e-4;    % Pas de temps [s]
tpsfin     = 0.05;    % Durée d'observation [s]
tmontee_PP = 0.001;   % Temps de montée de la charge [s]

liste_noeuds_record = noeud_bout;

% --- Cas 1 : Sans amortissement (Questions 11 à 14) ---
fprintf('\nCalcul dynamique Newmark implicite (sans amortissement, xi = 0)...\n');
CA_zero = sparse(NA, NA);
res_0   = newmark_implicite(KA, MA, CA_zero, fext, liste_ddl_libres, ...
                            dof, dtimp1, tpsfin, tmontee_PP, ...
                            gamma_N, beta_N, liste_noeuds_record);

% --- Cas 2 : Avec amortissement de Rayleigh (Questions 15 à 17) ---
xi_ray    = 0.05;                    % Pourcentage d'amortissement critique
T_obs     = 0.0413 - 0.0208;         % Période observée dans le TD [s]
omega_obs = 2 * pi / T_obs;
omega_1   = omega_obs;
omega_2   = 4 * omega_obs;

alpha_R = 2 * xi_ray * omega_1 * omega_2 / (omega_1 + omega_2);
beta_R  = 2 * xi_ray / (omega_1 + omega_2);
CA_ray  = alpha_R * MA + beta_R * KA;

fprintf('Calcul dynamique Newmark implicite (Rayleigh, xi = %.2f)...\n', xi_ray);
res_ray = newmark_implicite(KA, MA, CA_ray, fext, liste_ddl_libres, ...
                            dof, dtimp1, tpsfin, tmontee_PP, ...
                            gamma_N, beta_N, liste_noeuds_record);

%% Post-traitement dynamique (Questions 14, 16, 17)
t_lim = [res_0.tps(1), res_0.tps(end)];

% Figure 5 : Comparaison déplacements en bout de poutre vs statique
figure(5); clf;
plot(res_0.tps, res_0.DispCY(1, :), 'b-', 'LineWidth', 1.4); hold on;
plot(res_ray.tps, res_ray.DispCY(1, :), 'r-', 'LineWidth', 1.4);
plot(t_lim, [fleche_num,  fleche_num],  'k--', 'LineWidth', 1.2);
plot(t_lim, [fleche_theo, fleche_theo], 'g:',  'LineWidth', 1.5);
grid on;
xlabel('Temps [s]'); ylabel('Déplacement U_y [m]');
title(sprintf('Déplacement vertical en bout de poutre (noeud %d)', noeud_bout));
legend('Dynamique sans amortissement (\xi = 0)', ...
       sprintf('Dynamique Rayleigh (\\xi = %.2f)', xi_ray), ...
       'Statique EF', 'Statique analytique (Euler-Bernoulli)', ...
       'Location', 'best');

% Figure 6 : Effet de l'amortissement sur vitesses et accélérations (Q17)
figure(6); clf;
subplot(2, 1, 1);
plot(res_0.tps, res_0.VitCY(1, :), 'b-', 'LineWidth', 1.2); hold on;
plot(res_ray.tps, res_ray.VitCY(1, :), 'r-', 'LineWidth', 1.2);
grid on;
xlabel('Temps [s]'); ylabel('Vitesse V_y [m/s]');
title('Effet de l''amortissement de Rayleigh sur la vitesse en bout de poutre');
legend('\xi = 0', sprintf('\\xi = %.2f', xi_ray), 'Location', 'best');

subplot(2, 1, 2);
plot(res_0.tps, res_0.AccCY(1, :), 'b-', 'LineWidth', 1.2); hold on;
plot(res_ray.tps, res_ray.AccCY(1, :), 'r-', 'LineWidth', 1.2);
grid on;
xlabel('Temps [s]'); ylabel('Accélération A_y [m/s^2]');
title('Effet de l''amortissement de Rayleigh sur l''accélération en bout de poutre');
legend('\xi = 0', sprintf('\\xi = %.2f', xi_ray), 'Location', 'best');

% Figure 7 : Bilan énergétique
figure(7); clf;
subplot(2, 1, 1);
plot(res_0.tps, res_0.wint, 'b-', res_0.tps, res_0.wcin, 'r-', ...
     res_0.tps, res_0.wext, 'k--', res_0.tps, res_0.wint + res_0.wcin, 'g:', 'LineWidth', 1.3);
grid on; xlabel('Temps [s]'); ylabel('Energie [J]');
title('Bilan énergétique sans amortissement (\xi = 0)');
legend('W_{int}', 'W_{cin}', 'W_{ext}', 'W_{int} + W_{cin}', 'Location', 'best');

subplot(2, 1, 2);
plot(res_ray.tps, res_ray.wint, 'b-', res_ray.tps, res_ray.wcin, 'r-', ...
     res_ray.tps, res_ray.wamort, 'm-', res_ray.tps, res_ray.wext, 'k--', ...
     res_ray.tps, res_ray.wint + res_ray.wcin + res_ray.wamort, 'g:', 'LineWidth', 1.3);
grid on; xlabel('Temps [s]'); ylabel('Energie [J]');
title(sprintf('Bilan énergétique avec Rayleigh (\\xi = %.2f)', xi_ray));
legend('W_{int}', 'W_{cin}', 'W_{amort}', 'W_{ext}', 'Somme totale', 'Location', 'best');

%% ========================================================================
%  FONCTIONS LOCALES
% =========================================================================

function afficher_maillage(TA, XA, couleur, afficher_num)
    % Utilise plotelem externe si toutes ses dépendances sont dans le Path,
    % sinon trace directement le maillage en MATLAB natif.
    if exist('plotelem', 'file') == 2 && exist('cols', 'file') == 2 && exist('rows', 'file') == 2
        plotelem(TA, XA, couleur, afficher_num);
        return;
    end

    N_el = size(TA, 1);
    X_p  = zeros(4, N_el);
    Y_p  = zeros(4, N_el);
    for e = 1:N_el
        nds       = TA(e, 1:4);
        X_p(:, e) = XA(nds, 1);
        Y_p(:, e) = XA(nds, 2);
    end
    patch(X_p, Y_p, 'w', 'FaceColor', 'none', 'EdgeColor', couleur);

    if afficher_num == 1
        hold on;
        for e = 1:N_el
            xm = mean(X_p(:, e));
            ym = mean(Y_p(:, e));
            text(xm, ym, num2str(e), 'Color', couleur, 'FontSize', 7, ...
                 'HorizontalAlignment', 'center');
        end
    end
end

function coef = eval_F_PP(t, tmontee, effort)
    % Utilise F_PP externe si présente dans le Path, sinon rampe linéaire
    if exist('F_PP', 'file') == 2
        coef = F_PP(t, tmontee, effort);
    else
        if t <= 0
            coef = 0;
        elseif t < tmontee
            coef = effort * (t / tmontee);
        else
            coef = effort;
        end
    end
end

function res = newmark_implicite(KA, MA, CA, fext, liste_ddl_libres, ...
                                 dof, dtimp1, tpsfin, tmontee_PP, ...
                                 gamma_N, beta_N, liste_noeuds_record)

    NA   = size(KA, 1);
    nbdt = floor(tpsfin / dtimp1);
    nrec = length(liste_noeuds_record);

    % Opérateur dynamique : M_tilde = M + gamma*dt*C + beta*dt^2*K
    M_tilde     = MA + gamma_N * dtimp1 * CA + beta_N * (dtimp1^2) * KA;
    M_tilde_lib = M_tilde(liste_ddl_libres, liste_ddl_libres);

    % Factorisation LU unique hors boucle temporelle
    [L_m, U_m, P_m, Q_m] = lu(M_tilde_lib);

    % Initialisation cinématique à t = 0
    dep1 = zeros(NA, 1);
    vit1 = zeros(NA, 1);
    acc1 = zeros(NA, 1);
    f1n2 = zeros(NA, 1);

    % Tableaux de stockage
    tps_num = zeros(1, nbdt + 1);
    DispCX  = zeros(nrec, nbdt + 1);
    DispCY  = zeros(nrec, nbdt + 1);
    VitCX   = zeros(nrec, nbdt + 1);
    VitCY   = zeros(nrec, nbdt + 1);
    AccCX   = zeros(nrec, nbdt + 1);
    AccCY   = zeros(nrec, nbdt + 1);

    wint   = zeros(nbdt + 1, 1);
    wcin   = zeros(nbdt + 1, 1);
    wext   = zeros(nbdt + 1, 1);
    wamort = zeros(nbdt + 1, 1);

    for ii = 2:(nbdt + 1)
        dep1_old = dep1;
        vit1_old = vit1;
        acc1_old = acc1;
        fext_old = f1n2;

        t1 = (ii - 1) * dtimp1;
        tps_num(ii) = t1;

        % Force externe à t_{n+1}
        f1n2 = eval_F_PP(t1, tmontee_PP, 1) * fext;

        % Prédicteurs
        upn1 = dep1_old + dtimp1 * vit1_old + (dtimp1^2) * (0.5 - beta_N) * acc1_old;
        vpn1 = vit1_old + dtimp1 * (1 - gamma_N) * acc1_old;

        % Second membre et calcul de l'accélération a_{n+1}
        fsl1 = f1n2 - CA * vpn1 - KA * upn1;
        asl1 = zeros(NA, 1);
        asl1(liste_ddl_libres) = Q_m * (U_m \ (L_m \ (P_m * fsl1(liste_ddl_libres))));

        % Correcteurs
        vsl1 = vpn1 + gamma_N * dtimp1 * asl1;
        usl1 = upn1 + beta_N * (dtimp1^2) * asl1;

        acc1 = asl1;
        vit1 = vsl1;
        dep1 = usl1;

        % Enregistrement aux noeuds suivis
        for k = 1:nrec
            nd = liste_noeuds_record(k);
            DispCX(k, ii) = dep1((nd - 1) * dof + 1);
            DispCY(k, ii) = dep1((nd - 1) * dof + 2);
            VitCX(k, ii)  = vit1((nd - 1) * dof + 1);
            VitCY(k, ii)  = vit1((nd - 1) * dof + 2);
            AccCX(k, ii)  = acc1((nd - 1) * dof + 1);
            AccCY(k, ii)  = acc1((nd - 1) * dof + 2);
        end

        % Energies
        dU = dep1 - dep1_old;
        wint(ii)   = wint(ii - 1)   + 0.5 * (KA * dep1 + KA * dep1_old)' * dU;
        wcin(ii)   = wcin(ii - 1)   + 0.5 * ((vit1' * MA * vit1) - (vit1_old' * MA * vit1_old));
        wext(ii)   = wext(ii - 1)   + 0.5 * (f1n2 + fext_old)' * dU;
        wamort(ii) = wamort(ii - 1) + 0.5 * (CA * vit1 + CA * vit1_old)' * dU;
    end

    res.tps    = tps_num;
    res.DispCX = DispCX;  res.DispCY = DispCY;
    res.VitCX  = VitCX;   res.VitCY  = VitCY;
    res.AccCX  = AccCX;   res.AccCY  = AccCY;
    res.wint   = wint;    res.wcin   = wcin;
    res.wext   = wext;    res.wamort = wamort;
end

19,066 chars · codepastes.com