Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Errors with my code

1 visualización (últimos 30 días)
Madara Hettigama
Madara Hettigama el 12 de En. de 2020
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
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 comentario
Sindar
Sindar el 12 de En. de 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)

Respuestas (1)

Meg Noah
Meg Noah el 12 de En. de 2020
Editada: Meg Noah el 12 de En. de 2020
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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by