1. 海上追捕问题
现有一违法船只,以30km/h的速度向正北方向行驶,被我方海警船发现,海警船位于该违法船只正东方向20km处,海警船始终以朝向违法船只的方向行驶以进行追捕,速度为40km/h,违法船只速度大小方向不变,请绘制两船的运动曲线,并计算抓捕所用时间和距离。
由题目可知,假设违法船只的横纵坐标为: ( x w , y w ) (x_w,y_w) (xw,yw)。于是,可得到其曲线方程为:
{ x w = 0 , y w = 30 t ; \left\{\begin{aligned} &x_w = 0,\\ &y_w = 30t; \end{aligned}\right. {xw=0,yw=30t;
同理,我们假设海警船的坐标为: ( x p , y p ) (x_p,y_p) (xp,yp)。也得到其曲线方程为:
{ d x p = 30 ⋅ sin ( arctan ( x w − x p y w − y p ) ) , d y p = 30 ⋅ cos ( arctan ( x w − x p y w − y p ) ) , x p ( 0 ) = 20 , y p ( 0 ) = 0 ; \left\{\begin{aligned} &dx_p = 30\cdot \sin(\arctan(\frac{x_w-x_p}{y_w-y_p})),\\ &dy_p = 30\cdot \cos(\arctan(\frac{x_w-x_p}{y_w-y_p})),\\ &x_p(0) = 20,\\ &y_p(0) = 0;\\ \end{aligned}\right. ⎩⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎧dxp=30⋅sin(arctan(yw−ypxw−xp)),dyp=30⋅cos(arctan(yw−ypxw−xp)),xp(0)=20,yp(0)=0;
由此,将上述方程带入Matlab软件进行求解,具体代码如下:
clear
syms t x(t) y(t);
equation_3 = diff(x,t) == 40 * sin(atan((0 - x)/(30 * t - y)));
equation_4 = diff(y,t) == 40 * cos(atan((0 - x)/(30 * t - y)));
cond1 = x(0) == 20;
cond2 = y(0) == 0;
dsolve(equation_3,equation_4,cond1,cond2,t);
运行之后,Matlab无法找到解析解:
>> homework2_1
警告: Unable to find explicit solution.
> In dsolve (line 190)
In homework2_1 (line 11)
由于学艺不精,不知道怎么办,所以考虑使用数值方法进行求解,其代码如下:
clear all;
dt = 0.0001;
i = 1;
x_w(i) = 0;
y_w(i) = 30 * dt;
x_p(i) = 20;
y_p(i) = 0;
while(y_p(i) < y_w(i))
i = i + 1;
x_w(i) = 0;
y_w(i) = y_w(i-1) + 30 * dt;
v_x = 40 * sin(atan((x_w(i-1) - x_p(i-1))/(y_w(i-1) - y_p(i-1))));
v_y = 40 * cos(atan((x_w(i-1) - x_p(i-1))/(y_w(i-1) - y_p(i-1))));
x_p(i) = x_p(i-1) + v_x * dt;
y_p(i) = y_p(i-1) + v_y * dt;
end
plot(x_p,y_p);
hold on;
plot(x_w,y_w);
得到的轨迹图如下:
图中,红色曲线为违法船只的轨迹曲线,蓝色曲线为海警船的轨迹曲线。由数据可知,抓捕需要的时间 t c = 1.1430 h o u r t_c = 1.1430 \ hour tc=1.1430 hour;海警船所走距离 t c = 45.72 k m t_c = 45.72\ km tc=45.72 km;
2. 最小二乘法直线拟合
现有一组散点,其横纵坐标分别为:x = [1.3, 2.0, 3.6, 4.2, 5.3, 6.6, 7.8, 8.3, 9.1, 10.2], y =
[1.2, 3.2, 5.9, 6.5, 10.3, 11.2, 13.0, 14.6, 16.9, 18.3],请使用最小二乘法来进行直线拟合,并
将散点图与拟合后的直线绘制出来,输出拟合直线的k, b参数。
由题,Matlab代码如下:
clear all
x = [1.3, 2.0, 3.6, 4.2, 5.3, 6.6, 7.8, 8.3, 9.1, 10.2];
y = [1.2, 3.2, 5.9, 6.5, 10.3, 11.2, 13.0, 14.6, 16.9, 18.3];
fit = polyfit(x,y,1);
t = 0:0.01:15;
val = polyval(fit,t);
plot(t,val);
hold on;
plot(x,y,'*');
拟合得到的结果图:
图中,蓝色曲线为拟合得到曲线结果,红色星号为原数据点;此时拟合曲线的参数为: k = 1.8884 k = 1.8884 k=1.8884, b = − 0.9184 b = -0.9184 b=−0.9184。