错误,在不断发生

20视图(30天)
雅苒
雅苒 2023年5月3日22:59
回答: 图像分析 2023年5月4日2点46分
当我运行这段代码一切工作直到d部分,这个错误不断出现,虽然我之前定义的函数:
未识别的函数或变量“theta_explicit”。
错误hw4_405758924_p1(第138行)
情节(t theta_explicit b t theta_semi_implicit,‘r’, t, theta_implicit, ' g ');
% %部分
使用显式欧拉方法%摆物理问题
清晰的所有;关闭所有;
%定义常量
g = 9.8;%重力加速度(m / s ^ 2)
L = 2.5;%的字符串长度(米)
dt = 0.01;%时间步(s)
t_end = 20;%结束时间(s)
%初始条件
theta0 =π/ 2;%初始角度(弧度)
w0 = 0;%初始角速度(弧度/秒)
%预先分配数组
N =圆(t_end / dt);
theta_explicit = 0 (N + 1);
w_explicit = 0 (N + 1);
E_total = 0 (N + 1);
%设置初始条件
theta_explicit (1) = theta0;
w_explicit (1) = w0;
E_total (1) = g * L * (1-cos (theta_explicit (1)));
在每个时间步%计算θ和w
k = 1: N
w_explicit (k + 1) = w_explicit (k) L - g * *罪(theta_explicit (k)) * dt;
theta_explicit (k + 1) = theta_explicit (k) + w_explicit (k) * dt;
E_total (k + 1) = g * L * (1-cos (theta_explicit (k + 1))) + 0.5 * L ^ 2 * w_explicit (k + 1) ^ 2;
结束
%绘制结果
t = linspace (0 t_end N + 1);
情节(t, theta_explicit“b”t w_explicit“r”t E_total‘g’);
标题(“显式欧拉方法”);
包含(“时间(s)”);
ylabel (的角位置(弧度)/角速度(弧度/秒)/总能量每单位质量(J /公斤));
传奇(角位置的,的角速度,的总能量);
saveas (gcf“explicit_euler.png”);
% % b部分
使用semi-implicit欧拉方法%摆物理问题
清晰的所有;关闭所有;
%定义常量
g = 9.8;%重力加速度(m / s ^ 2)
L = 2.5;%的字符串长度(米)
dt = 0.01;%时间步(s)
t_end = 20;%结束时间(s)
%初始条件
theta0 =π/ 2;%初始角度(弧度)
w0 = 0;%初始角速度(弧度/秒)
%预先分配数组
N =圆(t_end / dt);
theta_semi_implicit = 0 (N + 1);
w_semi_implicit = 0 (N + 1);
E_total = 0 (N + 1);
%设置初始条件
theta_semi_implicit (1) = theta0;
w_semi_implicit (1) = w0;
E_total (1) = g * L * (1-cos (theta_semi_implicit (1)));
在每个时间步%计算θ和w
k = 1: N
w_semi_implicit (k + 1) = w_semi_implicit (k) L - g * *罪(theta_semi_implicit (k + 1) * dt;
theta_semi_implicit (k + 1) = theta_semi_implicit (k) + w_semi_implicit (k + 1) * dt;
E_total (k + 1) = g * L * (1-cos (theta_semi_implicit (k + 1))) + 0.5 * L ^ 2 * w_semi_implicit (k + 1) ^ 2;
结束
%绘制结果
t = linspace (0 t_end N + 1);
情节(t, theta_semi_implicit“b”t w_semi_implicit“r”t E_total‘g’);
标题(“Semi-Implicit欧拉方法”);
包含(“时间(s)”);
ylabel (角位置(弧度)/角速度')
% % c部分
%的常量
g = 9.8;% m / s ^ 2
L = 2.5;% m
dt = 0.01;% s
t_start = 0;% s
t_end = 20;% s
theta0 =π/ 2;% rad
n_steps =装天花板((t_end - t_start) / dt);
%初始化数组
theta_implicit = 0 (n_steps, 1);
w_implicit = 0 (n_steps, 1);
能量= 0 (n_steps, 1);
%初始条件
theta_implicit (1) = 5;% < < < theta0由价值;
w_implicit (1) = 0;%从其他
g = 9.80665;% < < <添加缺失值为g(米/秒^ 2)
L = 1;L % < < <由值
%使用隐式欧拉方法解决
我= 2:n_steps
w_implicit (i) = w_implicit(张)L - g * *罪(theta_implicit(张))* dt;
theta_implicit (i) = newton_raphson (theta_implicit(张),w_implicit(我),dt);
能源(i) = g * L * (1 - cos (theta_implicit(我)))+ 0.5 * (L * w_implicit(我))^ 2;
结束
%绘制
t = linspace (t_start t_end n_steps);
持有
情节(t, theta_implicit“r”,“线宽”,2)
标题(“摆角位置”)
包含(“时间(s)”)
ylabel (“角位置(rad)”)
传奇(“明确”,“Semi-implicit”,“隐式”)
saveas (gcf“pendulum_position.png”)
图()
持有
情节(t、能源、“r”,“线宽”,2)
标题(摆的总能量)
包含(“时间(s)”)
ylabel (的能量每单位质量(J /公斤))
传奇(“明确”,“Semi-implicit”,“隐式”)
警告:忽略额外的传奇条目。
saveas (gcf“pendulum_energy.png”)
% % d部分
%绘制角位置
图;
情节(t, theta_explicit“b”t theta_semi_implicit“r”t theta_implicit‘g’);
未识别的函数或变量“theta_explicit”。
标题(质量和时间的角位置);
包含(“时间(s)”);
ylabel (的角位置(弧度));
传奇(“显式欧拉”,“Semi-Implicit欧拉”,“隐式欧拉”);
saveas (gcf“angular_position.png”);
% % e部分
%计算总能量每单位质量为每个方法
g = 9.8;%重力加速度(m / s ^ 2)
E_explicit = g * L * (1 - cos (theta_explicit)) + 0.5 * L ^ 2 * w_explicit。^ 2;
E_semi_implicit = g * L * (1 - cos (theta_semi_implicit)) + 0.5 * L ^ 2 * w_semi_implicit。^ 2;
E_implicit = g * L * (1 - cos (theta_implicit)) + 0.5 * L ^ 2 * w_implicit。^ 2;
%绘制总能量每单位质量为每个方法
图;
情节(t, E_explicit的r -,“线宽”,1.5);
持有;
情节(t, E_semi_implicit“b -”,“线宽”,1.5);
情节(t, E_implicit“g -”,“线宽”,1.5);
包含(“时间(s)”);
ylabel (每单位质量的总能量(J /公斤));
标题(“单摆的总能量每单位质量”);
传奇(“显式欧拉”,“Semi-Implicit欧拉”,“隐式欧拉”);
网格;
%保存情节
saveas (gcf“pendulum_energy.png”);
%牛顿迭代方法解决隐式欧拉方程
函数[theta_next] = newton_raphson (theta_curr omega_curr, dt)
g = 9.80665;% < < <添加缺失值为g(米/秒^ 2)
L = 1;L % < < <由值
theta_next = theta_curr;%初始猜测
f = @ (theta_next) theta_next theta_curr - dt * omega_curr (dt ^ 2/2) * (L - g * *罪(theta_next));
df = @ (theta_next) 1 - (dt ^ 2/2) * (L - g * * cos (theta_next));
i = 1:5% 5的迭代收敛
theta_next = theta_next - f (theta_next) / df (theta_next);
结束
结束
1评论
沃尔特·罗伯森
沃尔特·罗伯森 2023年5月3日23:39
清晰的所有;关闭所有;
在中间的代码不是给你带来任何好处。

登录置评。

答案(1)

图像分析
图像分析 2023年5月4日2点46分
在46行,有一个明确的,消除变量:
% % b部分
使用semi-implicit欧拉方法%摆物理问题
清晰的所有;关闭所有;
摆脱,清除所有关闭所有行。有不少更多的错误。我相信我已经固定的给你。
% %部分
使用显式欧拉方法%摆物理问题
清晰的所有;
关闭所有;
clc;
%定义常量
g = 9.8;%重力加速度(m / s ^ 2)
L = 2.5;%的字符串长度(米)
dt = 0.01;%时间步(s)
t_end = 20;%结束时间(s)
%初始条件
theta0 =π/ 2;%初始角度(弧度)
w0 = 0;%初始角速度(弧度/秒)
%预先分配数组
N =圆(t_end / dt);
theta_explicit = 0 (N + 1);
w_explicit = 0 (N + 1);
E_total = 0 (N + 1);
%设置初始条件
theta_explicit (1) = theta0;
w_explicit (1) = w0;
E_total (1) = g * L * (1-cos (theta_explicit (1)));
在每个时间步%计算θ和w
k = 1: n - 1
w_explicit (k + 1) = w_explicit (k) L - g * *罪(theta_explicit (k)) * dt;
theta_explicit (k + 1) = theta_explicit (k) + w_explicit (k) * dt;
E_total (k + 1) = g * L * (1-cos (theta_explicit (k + 1))) + 0.5 * L ^ 2 * w_explicit (k + 1) ^ 2;
结束
%绘制结果
t = linspace (0 t_end N + 1) ';
图(“名字”,“甲方”,“NumberTitle”,“关闭”)
情节(t, theta_explicit“b”t w_explicit“r”t E_total‘g’);
网格;
标题(“显式欧拉方法”);
包含(“时间(s)”);
ylabel (的角位置(弧度)/角速度(弧度/秒)/总能量每单位质量(J /公斤));
传奇(角位置的,的角速度,的总能量);
drawnow;%的力量直接屏幕更新。
saveas (gcf“explicit_euler.png”);
% % b部分
使用semi-implicit欧拉方法%摆物理问题
%定义常量
g = 9.8;%重力加速度(m / s ^ 2)
L = 2.5;%的字符串长度(米)
dt = 0.01;%时间步(s)
t_end = 20;%结束时间(s)
%初始条件
theta0 =π/ 2;%初始角度(弧度)
w0 = 0;%初始角速度(弧度/秒)
%预先分配数组
N =圆(t_end / dt);
theta_semi_implicit = 0 (N + 1);
w_semi_implicit = 0 (N + 1);
E_total = 0 (N + 1);
%设置初始条件
theta_semi_implicit (1) = theta0;
w_semi_implicit (1) = w0;
E_total (1) = g * L * (1-cos (theta_semi_implicit (1)));
在每个时间步%计算θ和w
k = 1: n - 1
w_semi_implicit (k + 1) = w_semi_implicit (k) L - g * *罪(theta_semi_implicit (k + 1) * dt;
theta_semi_implicit (k + 1) = theta_semi_implicit (k) + w_semi_implicit (k + 1) * dt;
E_total (k + 1) = g * L * (1-cos (theta_semi_implicit (k + 1))) + 0.5 * L ^ 2 * w_semi_implicit (k + 1) ^ 2;
结束
%绘制结果
t = linspace (0 t_end N + 1) ';
图(“名字”,“乙方”,“NumberTitle”,“关闭”)
情节(t, theta_semi_implicit“b”t w_semi_implicit“r”t E_total‘g’);
网格;
标题(“Semi-Implicit欧拉方法”);
包含(“时间(s)”);
ylabel (角位置(弧度)/角速度')
drawnow;%的力量直接屏幕更新。
% % c部分
%的常量
g = 9.8;% m / s ^ 2
L = 2.5;% m
dt = 0.01;% s
t_start = 0;% s
t_end = 20;% s
theta0 =π/ 2;% rad
n_steps = N + 1;%装天花板((t_end - t_start) / dt);
%初始化数组
theta_implicit = 0 (n_steps, 1);
w_implicit = 0 (n_steps, 1);
能量= 0 (n_steps, 1);
%初始条件
theta_implicit (1) = 5;% < < < theta0由价值;
w_implicit (1) = 0;%从其他
g = 9.80665;% < < <添加缺失值为g(米/秒^ 2)
L = 1;L % < < <由值
%使用隐式欧拉方法解决
k = 2: n_steps
w_implicit (k) = w_implicit (k - 1) L - g * *罪(theta_implicit (k - 1)) * dt;
theta_implicit (k) = newton_raphson (theta_implicit (k - 1), w_implicit (k), dt);
能源(k) = g * L * (1 - cos (theta_implicit (k))) + 0.5 * (L * w_implicit (k)) ^ 2;
结束
%绘制
图(“名字”,“c”部分,“NumberTitle”,“关闭”)
t = linspace (t_start t_end n_steps);
持有
情节(t, theta_implicit“r”,“线宽”,2)
网格;
标题(“摆角位置”)
包含(“时间(s)”)
ylabel (“角位置(rad)”)
saveas (gcf“pendulum_position.png”)
drawnow;%的力量直接屏幕更新。
持有
情节(t、能源、“r”,“线宽”,2)
网格;
标题(摆的总能量)
包含(“时间(s)”)
ylabel (的能量每单位质量(J /公斤))
传奇(“theta_implicit”,“能源”)
saveas (gcf“pendulum_energy.png”)
drawnow;%的力量直接屏幕更新。
% % d部分
%绘制角位置
图(“名字”,“d部分”,“NumberTitle”,“关闭”)
情节(t, theta_explicit“b”t theta_semi_implicit“r”t theta_implicit‘g’);
网格;
标题(质量和时间的角位置);
包含(“时间(s)”);
ylabel (的角位置(弧度));
传奇(“显式欧拉”,“Semi-Implicit欧拉”,“隐式欧拉”);
saveas (gcf“angular_position.png”);
drawnow;%的力量直接屏幕更新。
% % e部分
%计算总能量每单位质量为每个方法
g = 9.8;%重力加速度(m / s ^ 2)
E_explicit = g * L * (1 - cos (theta_explicit)) + 0.5 * L ^ 2 * w_explicit。^ 2;
E_semi_implicit = g * L * (1 - cos (theta_semi_implicit)) + 0.5 * L ^ 2 * w_semi_implicit。^ 2;
E_implicit = g * L * (1 - cos (theta_implicit)) + 0.5 * L ^ 2 * w_implicit。^ 2;
%绘制总能量每单位质量为每个方法
图(“名字”,“e”部分,“NumberTitle”,“关闭”)
情节(t, E_explicit的r -,“线宽”,1.5);
网格;
持有;
情节(t, E_semi_implicit“b -”,“线宽”,1.5);
情节(t, E_implicit“g -”,“线宽”,1.5);
包含(“时间(s)”);
ylabel (每单位质量的总能量(J /公斤));
标题(“单摆的总能量每单位质量”);
传奇(“显式欧拉”,“Semi-Implicit欧拉”,“隐式欧拉”);
网格;
drawnow;%的力量直接屏幕更新。
%保存情节
saveas (gcf“pendulum_energy.png”);
uiwait (helpdlg (“干!”))
% = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
%牛顿迭代方法解决隐式欧拉方程
函数[theta_next] = newton_raphson (theta_curr omega_curr, dt)
g = 9.80665;% < < <添加缺失值为g(米/秒^ 2)
L = 1;L % < < <由值
theta_next = theta_curr;%初始猜测
f = @ (theta_next) theta_next theta_curr - dt * omega_curr (dt ^ 2/2) * (L - g * *罪(theta_next));
df = @ (theta_next) 1 - (dt ^ 2/2) * (L - g * * cos (theta_next));
k = 1:5% 5的迭代收敛
theta_next = theta_next - f (theta_next) / df (theta_next);
结束
结束

类别

找到更多的在线性代数帮助中心文件交换

标签

s manbetx 845


释放

R2020b

社区寻宝

找到宝藏在MATLAB中央,发现社区如何帮助你!

开始狩猎!