Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Errors with my code

1 回表示 (過去 30 日間)
Madara Hettigama
Madara Hettigama 2020 年 1 月 12 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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
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
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)');
SolutionsToSine.png

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by