When I run my code, I keep getting this error: "Undefined function or variable 'y'."
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Nauryzbay
le 26 Mai 2023
Commenté : Nauryzbay
le 30 Mai 2023
close all;clc
tspan = [0 1];
y0(1)=2;
y0(2)=1;
y0(3)=3;
source = 0;
tol = 1e-3;%Критерии допуска для определения наилучшей стоимости Мой критерий остановки - Tol= 1e-3.
error = 1.0;%Начальная ошибка запуска
while error > tol%ЗАПУСТИТЕ ЦИКЛ WHILE
sol = ode45(@(t,y) ode2(t,y,source), [0 1], y0);% ВЫЗЫВАЕМ СОЛВЕР BVP4C:
t = linspace(0,1,100);
source_new = trapz(t,y(3,:));
error = abs(source_new - source);
source = source_new;
end
figure(1);
subplot(3,1,1)
plot(sol.x,sol.y(1,:),'color','r','Linewidth',1.2);
grid on
hold on
xlabel('\bf t'); ylabel('$$y$$','interpreter','latex','fontsize',16);
subplot(3,1,2)
plot(sol.x,sol.y(2,:),'color','r','Linewidth',1.2);
grid on
hold on
xlabel('\bf t'); ylabel('$$\dot{y}$$','interpreter','latex','fontsize',16);
subplot(3,1,3)
plot(sol.x,sol.y(3,:),'color','r','Linewidth',1.2);
grid on
hold on
xlabel('\bf t'); ylabel('$$y"$$','interpreter','latex','fontsize',16);
function dy = ode2(~,y,source)%y''+y'=1+int^1_0(y'*x)dx;y(0) = 2;y(1) = 4;
m=0.05;
dy = zeros(3,1); % создает нулевой вектор-столбец
dy(1)=y(2);
dy(2)=y(3);
dy(3)=-3/m*y(3)+1/(m.^2)*(1-2*y(2)+4.*source);
end
Undefined function or variable 'y'.
Error in IDTinitial6 (line 14)
source_new = trapz(t,y(3,:));
0 commentaires
Réponse acceptée
Walter Roberson
le 26 Mai 2023
Later you keep referring to sol.y so perhaps the reference to y should instead be to sol.y
7 commentaires
Plus de réponses (1)
Torsten
le 29 Mai 2023
Modifié(e) : Torsten
le 29 Mai 2023
syms t y(t)
syms s real
m = 0.05;
Dy = diff(y,t);
D2y = diff(y,t,2);
D3y = diff(y,t,3);
eqn = D3y -( -3/m*D2y + 1/m^2*(1-2*Dy+4*s)) ;
sol_ode = dsolve(eqn)
vars = symvar(sol_ode)
cond1 = subs(sol_ode,t,0) == 2;
cond2 = subs(diff(sol_ode,t),t,0) == 1;
cond3 = subs(diff(sol_ode,t,2),t,0) == 3;
cond4 = subs(diff(sol_ode,t),t,1) - subs(diff(sol_ode,t),t,0) == s;
sol_cond = solve([cond1,cond2,cond3,cond4])
double(sol_cond.s)
sol_ode = simplify(subs(sol_ode,[vars(1),vars(2),vars(3),vars(4)],[sol_cond.C1,sol_cond.C2,sol_cond.C3,sol_cond.s]))
figure(1)
fplot(sol_ode,[0 1])
figure(2)
fplot(diff(sol_ode,t),[0 1])
figure(3)
fplot(diff(sol_ode,t,2),[0 1])
simplify(diff(sol_ode,t,3) -( -3/m*diff(sol_ode,t,2) + 1/m^2*(1-2*diff(sol_ode,t)+4*(subs(diff(sol_ode,t),t,1)-subs(diff(sol_ode,t),t,0)))))
2 commentaires
Torsten
le 29 Mai 2023
A different method to solve the problem:
y0(1)=2;
y0(2)=1;
y0(3)=3;
source = 0.0;
solinit = bvpinit(linspace(0,1,100),y0,source);
sol = bvp4c(@mat4ode, @mat4bc, solinit);
sol.parameters
figure(1)
plot(sol.x,sol.y(1,:))
figure(2)
plot(sol.x,sol.y(2,:))
figure(3)
plot(sol.x,sol.y(3,:))
function dydx = mat4ode(x,y,source) % equation being solved
m = 0.05;
dydx = [y(2);y(3);-3/m*y(3)+1/(m.^2)*(1-2*y(2)+4.*source)];
end
function res = mat4bc(ya,yb,source) % boundary conditions
res = [ya(1)-2.0;ya(2)-1.0;ya(3)-3.0;yb(2)-1.0-source];
end
Voir également
Catégories
En savoir plus sur Trimming and Linearization dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!








