Info
此问题已关闭。 请重新打开它进行编辑或回答。
Errors with my code
1 次查看(过去 30 天)
显示 更早的评论
What's going wrong with this code?
T1=[0:0.001:2];
Y1=(-1/4)*cos(4*T1);
%euler approximation
y= zeros(40,1);
t=linspace(0.05,2,40);
y(1)=-0.25;
for i=1:39;
t(i+1)=t(i)+0,05;
y(i+1)=y(i)+0.05.*(sin(4*t(i)))
end
%taylor approximation
y=zeros(20,1);
T=linspace(0.1,2,20);
h=0.1;
Y(1)=-0.25;
for n=1:19;
Y(n+1)=Y(n)+h*sin(4*T(n))+2*(h.^2)*cos(4*T(n))-(8*h.^3*sin(4*T(n))/3-(8*h.^4*cos(4*T(n)))/3)
end
figure(7)
plot(T1, Y1, 'red');
plot(t,y, 'blue');
hold on;
grid on
legend('taylor approximation','analytical solution', euler approximation');
xlabel('time(s)');
ylabel('position(m)’)
1 个评论
Sindar
2020-1-12
Could you add more info? Are you getting an error message? If so, copy it here. What's supposed to happen? Can you copy the commands together (SHIFT+scroll up, then select and copy), and put them in a code block (the "code" button on this editor)
回答(1 个)
Meg Noah
2020-1-12
编辑:Meg Noah
2020-1-12
You redefined y to zero it out and at a different dimension than t. Also some typos in the annotation.
T1=[0:0.001:2];
Y1=(-1/4)*cos(4*T1);
%euler approximation
y= zeros(1,40);
t=linspace(0.05,2,40);
y(1)=-0.25;
for i=1:39
t(i+1)=t(i)+0.05;
y(i+1)=y(i)+0.05.*(sin(4*t(i)));
end
%taylor approximation
Y=zeros(20,1);
T=linspace(0.1,2,20);
h=0.1;
Y(1)=-0.25;
for n=1:19
Y(n+1)=Y(n)+h*sin(4*T(n))+2*(h.^2)*cos(4*T(n))-(8*h.^3*sin(4*T(n))/3-(8*h.^4*cos(4*T(n)))/3);
end
% visualize the results
figure(7)
plot(T1, Y1, 'r');
hold on;
plot(t,y, 'b');
plot(T,Y, 'g');
grid on
legend('analytical solution', 'euler approximation','taylor approximation');
xlabel('time(s)');
ylabel('position (m)');

0 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!