我继续这个错误:错误:函数定义在脚本中必须出现在文件的末尾。后将所有报表函数定义之前……

50个视图(30天)
我继续这个错误:错误:文件:hw4_405758924_p1。线:104列:1函数定义在脚本中必须出现在文件的末尾。“newton_raphson”后将所有报表函数定义在第一个本地函数定义。
main ()
警告:忽略额外的传奇条目。
警告:忽略额外的传奇条目。
函数主要
%的常量
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);
%初始化数组
θ= 0 (n_steps, 1);
ω= 0 (n_steps, 1);
能量= 0 (n_steps, 1);
%初始条件
θ(1)= theta0;
ω(1)= 0;%从其他
%使用隐式欧拉方法解决
我= 2:n_steps
ω(i) =ω(张)L - g * *罪(θ(张))* dt;
θ(i) = newton_raphson(θ(张),ω(i), dt);
能源(i) = g * L * (1 - cos(θ(i))) + 0.5 * (L *ω(i)) ^ 2;
结束
%绘制
t = linspace (t_start t_end n_steps);
持有
情节(t,θ,“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”)
%牛顿迭代方法解决隐式欧拉方程
函数[theta_next] = newton_raphson (theta_curr omega_curr, dt)
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)

Les贝克汉姆
Les贝克汉姆 2023年5月3日18:16
你必须有一些代码在您的脚本 结束 ,你没有。确保函数定义是最后的事情在你的脚本文件。
代码后作品提供一些未定义的变量的值。
我不知道你为什么有3个传奇条目,因为你只是在你的阴谋策划一个曲线。也许你打算添加更多的情节。
%的常量
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);
%初始化数组
θ= 0 (n_steps, 1);
ω= 0 (n_steps, 1);
能量= 0 (n_steps, 1);
%初始条件
θ(1)= 5;% < < < theta0由价值;
ω(1)= 0;%从其他
g = 9.80665;% < < <添加缺失值为g(米/秒^ 2)
L = 1;L % < < <由值
%使用隐式欧拉方法解决
我= 2:n_steps
ω(i) =ω(张)L - g * *罪(θ(张))* dt;
θ(i) = newton_raphson(θ(张),ω(i), dt);
能源(i) = g * L * (1 - cos(θ(i))) + 0.5 * (L *ω(i)) ^ 2;
结束
%绘制
t = linspace (t_start t_end n_steps);
持有
情节(t,θ,“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”)
%牛顿迭代方法解决隐式欧拉方程
函数[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);
结束
结束

标签

s manbetx 845


释放

R2020b

社区寻宝

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

开始狩猎!