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