2024年数学建模A题(1——3问代码)

2024年数学建模A题(1——3问代码)

第一问 and 第二问


clc;
clear;
close all;

%% 参数
A = (0.55/(2*pi))^2;
R = @(theta) 0.55/(2*pi)*theta; % 极径
numBody = 223; % 身体节数
head_theta0 = 32*pi; % 初始龙头theta

% 定义T(theta)
syms theta
T = theta*sqrt(theta^2+1) + log(sqrt(theta^2+1) + theta);
C = subs(T, theta, head_theta0);

% 矩形参数
front_offset = 0.275;
side_offset  = 0.15;

%% 螺旋线轨迹
theta_vals = linspace(0, head_theta0, 2000);
spiral_X = R(theta_vals).*cos(theta_vals);
spiral_Y = R(theta_vals).*sin(theta_vals);

%% 动画循环
figure;
axis equal;
grid on;
xlabel('X'); ylabel('Y');
title('龙身体随时间运动的动画');

collision_detected = false;
for t = 0:50:500
% for t = 400:1:450
% for t = 412:0.01:413
% for t = 412.473837:0.000001:412.473838  % 时间循环
    clf; hold on; axis equal; grid on;
    xlabel('X'); ylabel('Y');
    title(['时间 t = ', num2str(t, '%.7f'), ' 秒']);
    
    % 绘制螺旋线
    plot(spiral_X, spiral_Y, 'k--', 'LineWidth', 1);
    
    % 计算龙头theta
    question = T == C - 4*pi/0.55*t;
    head_theta = double(vpasolve(question, theta, [0, head_theta0]));
    head_X = double(R(head_theta) * cos(head_theta));
    head_Y = double(R(head_theta) * sin(head_theta));
    
    % 初始化骨架
    center_X_list = head_X;
    center_Y_list = head_Y;
    X = head_theta;
    
    % 绘制龙头点
    plot(head_X, head_Y, 'ro', 'MarkerFaceColor','r');
    
    % 存储身体矩形
    bodyRects = {};
    
    %% 生成身体
    for i = 1:numBody
        if i == 1
            L = 2.86; rect_length = 3.41; % 龙头节
        else
            L = 1.65; rect_length = 2.2; % 后续节
        end
        
        % 求解下一个关节
        syms Y
        eqn = A*(Y^2 + X^2) - L^2 == 2*A*Y*X*cos(Y - X);
        solution = double(vpasolve(eqn, Y, [X, X+pi/2]));
        L_X = R(solution) * cos(solution);
        L_Y = R(solution) * sin(solution);
        
        % 骨架点
        plot(L_X, L_Y, 'bo', 'MarkerFaceColor','b');
        center_X_list = [center_X_list, L_X];
        center_Y_list = [center_Y_list, L_Y];
        
        % 矩形方向
        dx = L_X - center_X_list(end-1);
        dy = L_Y - center_Y_list(end-1);
        len = sqrt(dx^2 + dy^2);
        ux = dx / len; uy = dy / len;
        vx = -uy; vy = ux;
        
        % 矩形顶点(把手在矩形内部)
        A_point = [center_X_list(end-1) - ux*front_offset + vx*side_offset, ...
                   center_Y_list(end-1) - uy*front_offset + vy*side_offset];
        B_point = [center_X_list(end-1) - ux*front_offset - vx*side_offset, ...
                   center_Y_list(end-1) - uy*front_offset - vy*side_offset];
        C_point = [L_X + ux*front_offset - vx*side_offset, ...
                   L_Y + uy*front_offset - vy*side_offset];
        D_point = [L_X + ux*front_offset + vx*side_offset, ...
                   L_Y + uy*front_offset + vy*side_offset];
        
        % 绘制矩形
        rectangle_X = [A_point(1), B_point(1), C_point(1), D_point(1), A_point(1)];
        rectangle_Y = [A_point(2), B_point(2), C_point(2), D_point(2), A_point(2)];
        plot(rectangle_X, rectangle_Y, 'b-', 'LineWidth', 1.2);
        
        % 存储身体矩形
        if i > 1  % 不存储龙头矩形
            bodyRects{end+1} = polyshape(rectangle_X, rectangle_Y);
        else
            % 龙头前端两个顶点 A 和 B
            head_front_pts = [A_point; B_point];  % 2x2矩阵
        end
        
        % 更新骨架
        X = solution;
    end
    
    % 绘制骨架线
    plot(center_X_list, center_Y_list, 'r-', 'LineWidth', 1.5);
    
    %% 碰撞检测(只检测龙头 A、B 两个顶点)
    for k = 1:length(bodyRects)
        if any(isinterior(bodyRects{k}, head_front_pts(:,1), head_front_pts(:,2)))
            disp(['发生碰撞,时间 t = ', num2str(t, '%.6f'), ' 秒']);
            plot(head_front_pts(:,1), head_front_pts(:,2), 'ro', 'MarkerFaceColor','r'); % 高亮碰撞点
            collision_detected = true;
            break;
        end
    end
    
    drawnow;
    if collision_detected
        break;  % 停止动画
    end
end

第三问

clc;
clear;
close all;

%% 参数
A = (0.4503/(2*pi))^2;
R = @(theta) 0.4503/(2*pi)*theta; % 极径
numBody = 22; % 身体节数
head_theta0 = 32*pi; % 初始龙头theta

% 定义T(theta)
syms theta
T = theta*sqrt(theta^2+1) + log(sqrt(theta^2+1) + theta);
C = subs(T, theta, head_theta0);

% 矩形参数
front_offset = 0.275;
side_offset  = 0.15;

%% 螺旋线轨迹
theta_vals = linspace(0, head_theta0, 2000);
spiral_X = R(theta_vals).*cos(theta_vals);
spiral_Y = R(theta_vals).*sin(theta_vals);

%% 动画循环
figure;
axis equal;
grid on;
xlabel('X'); ylabel('Y');
title('龙身体随时间运动的动画');

collision_detected = false;
% for t = 0:50:500
% for t = 400:1:450
% for t = 412:0.01:413
for t = 0:10:400 % 时间循环
    clf; hold on; axis equal; grid on;
    xlabel('X'); ylabel('Y');
    title(['时间 t = ', num2str(t, '%.7f'), ' 秒']);
    
    % 绘制螺旋线
    plot(spiral_X, spiral_Y, 'k--', 'LineWidth', 1);
    
    % 计算龙头theta
    question = T == C - 4*pi/0.4503*t;
    head_theta = double(vpasolve(question, theta, [0, head_theta0]));
    head_X = double(R(head_theta) * cos(head_theta));
    head_Y = double(R(head_theta) * sin(head_theta));
    
    % 初始化骨架
    center_X_list = head_X;
    center_Y_list = head_Y;
    X = head_theta;
    
    % 绘制龙头点
    plot(head_X, head_Y, 'ro', 'MarkerFaceColor','r');
    ans = sqrt(head_Y ^ 2 + head_X ^ 2); 
    % 存储身体矩形
    bodyRects = {};
    
    %% 生成身体
    for i = 1:numBody
        if i == 1
            L = 2.86; rect_length = 3.41; % 龙头节
        else
            L = 1.65; rect_length = 2.2; % 后续节
        end
        
        % 求解下一个关节
        syms Y
        eqn = A*(Y^2 + X^2) - L^2 == 2*A*Y*X*cos(Y - X);
        solution = double(vpasolve(eqn, Y, [X, X+pi/2]));
        L_X = R(solution) * cos(solution);
        L_Y = R(solution) * sin(solution);
        
        % 骨架点
        plot(L_X, L_Y, 'bo', 'MarkerFaceColor','b');
        center_X_list = [center_X_list, L_X];
        center_Y_list = [center_Y_list, L_Y];
        
        % 矩形方向
        dx = L_X - center_X_list(end-1);
        dy = L_Y - center_Y_list(end-1);
        len = sqrt(dx^2 + dy^2);
        ux = dx / len; uy = dy / len;
        vx = -uy; vy = ux;
        
        % 矩形顶点(把手在矩形内部)
        A_point = [center_X_list(end-1) - ux*front_offset + vx*side_offset, ...
                   center_Y_list(end-1) - uy*front_offset + vy*side_offset];
        B_point = [center_X_list(end-1) - ux*front_offset - vx*side_offset, ...
                   center_Y_list(end-1) - uy*front_offset - vy*side_offset];
        C_point = [L_X + ux*front_offset - vx*side_offset, ...
                   L_Y + uy*front_offset - vy*side_offset];
        D_point = [L_X + ux*front_offset + vx*side_offset, ...
                   L_Y + uy*front_offset + vy*side_offset];
        
        % 绘制矩形
        rectangle_X = [A_point(1), B_point(1), C_point(1), D_point(1), A_point(1)];
        rectangle_Y = [A_point(2), B_point(2), C_point(2), D_point(2), A_point(2)];
        plot(rectangle_X, rectangle_Y, 'b-', 'LineWidth', 1.2);
        
        % 存储身体矩形
        if i > 1  % 不存储龙头矩形
            bodyRects{end+1} = polyshape(rectangle_X, rectangle_Y);
        else
            % 龙头前端两个顶点 A 和 B
            head_front_pts = [A_point; B_point];  % 2x2矩阵
        end
        
        % 更新骨架
        X = solution;
    end
    % 绘制骨架线
    plot(center_X_list, center_Y_list, 'r-', 'LineWidth', 1.5);  
    %% 碰撞检测(只检测龙头 A、B 两个顶点)
    for k = 1:length(bodyRects)
        if any(isinterior(bodyRects{k}, head_front_pts(:,1), head_front_pts(:,2)))
            disp(['发生碰撞,时间 t = ', num2str(t, '%.6f'), ' 秒']);
            plot(head_front_pts(:,1), head_front_pts(:,2), 'ro', 'MarkerFaceColor','r'); % 高亮碰撞点
            collision_detected = true;
            break;
        end
    end
    drawnow;
    if collision_detected
        break;  % 停止动画
    end
end
fprintf('%.15f',ans);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

壹Y.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值