Error using plot Vectors must be the same length.
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Error using plot
Vectors must be the same length.
Error in midpointfinal (line 33)
plot(t,y_m,'-o')
clc; clear all;
h=0.01;
Tmax = 2; % Maximum time
Tmin=1; %Minimum time
n = (Tmax-Tmin)/ h; % Maximum number of steps
%n=(t(2)-t(1))/h;
t = linspace(0,1,n+1);;
alpha=0.5;
mu=20;
%initials%
y_m(1)=2;
y_m(2)=exp(20.*h)+cos(h);
f_m(1)=20.*(y_m(1)-cos(t(1)))-sin(t(1));
f_m(2)=20.*(y_m(2)-cos(t(2)))-sin(t(2));
% Analytical solution of the differential equation
exact = @(t) exp(mu*t)+cos(t);
plot(t,exact(t));
hold
%Numerical solution
%f= @(t,y) mu*(y-cos(t))-sin(t); % Governing system of equations
%Midpoint Two step method%
for i=3:n
y_m(i)=y_m(i-2)+2.*h.*f_m(i-1);
f_m(i)=20.*(y_m(i)-cos(t(i)))-sin(t(i));
end
plot(t,exact(t))
hold
plot(t,y_m,'-o')
legend('Exact Solution','Leapfrog Solution','Location','NorthEast')
xlabel('t')
ylabel('y')
2 commentaires
Atsushi Ueno
le 24 Mai 2022
Vector t and y_m must be the same length.
h = 0.01;
n = 100; % Maximum number of steps
t = linspace(0,1,n+1);
y_m(1)=2;
y_m(2)=exp(20.*h)+cos(h);
f_m(1)=20.*(y_m(1)-cos(t(1)))-sin(t(1));
f_m(2)=20.*(y_m(2)-cos(t(2)))-sin(t(2));
for i=3:n
y_m(i)=y_m(i-2)+2.*h.*f_m(i-1);
f_m(i)=20.*(y_m(i)-cos(t(i)))-sin(t(i));
end
size(t)
size(y_m)
Réponse acceptée
David Hill
le 24 Mai 2022
t = linspace(0,1,n);%vector t needs to be same length as y_m
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Ordinary Differential Equations dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!