Errors with my code

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 Kommentar

Sindar
Sindar am 12 Jan. 2020
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)

Antworten (1)

Meg Noah
Meg Noah am 12 Jan. 2020
Bearbeitet: Meg Noah am 12 Jan. 2020

0 Stimmen

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

Diese Frage ist geschlossen.

Gefragt:

am 12 Jan. 2020

Geschlossen:

am 20 Aug. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by