When I run my code, I keep getting this error: "Undefined function or variable 'y'."

1 次查看(过去 30 天)
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,:));

采纳的回答

Walter Roberson
Walter Roberson 2023-5-26
Later you keep referring to sol.y so perhaps the reference to y should instead be to sol.y
  7 个评论
Torsten
Torsten 2023-5-29
编辑:Torsten 2023-5-29
... which is y2(1) - y2(0) = y2(1) - 1.
But this also doesn't seem to help - the iteration diverges.
See my symbolic solution and the boundary value solution below.

请先登录,再进行评论。

更多回答(1 个)

Torsten
Torsten 2023-5-29
编辑:Torsten 2023-5-29
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)
sol_ode = 
vars = symvar(sol_ode)
vars = 
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])
sol_cond = struct with fields:
C1: 1663/800 C2: -(6*exp(20) + 7*exp(40))/(800*(exp(40) - 4*exp(20) + 2)) C3: (17*exp(40) + 6)/(400*(exp(40) - 4*exp(20) + 2)) s: (10*exp(40) - 23*exp(20) + 13)/(20*(exp(40) - 4*exp(20) + 2))
double(sol_cond.s)
ans = 0.5000
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]))
sol_ode = 
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)))))
ans = 
0
  2 个评论
Torsten
Torsten 2023-5-29
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 = struct with fields:
solver: 'bvp4c' x: [0 0.0101 0.0202 0.0303 0.0404 0.0505 0.0606 0.0707 0.0808 0.0909 0.1010 0.1111 0.1212 0.1313 0.1414 0.1515 0.1616 0.1717 0.1818 0.1919 0.2020 0.2121 0.2222 0.2323 0.2424 0.2525 … ] y: [3×100 double] yp: [3×100 double] parameters: 0.5000 stats: [1×1 struct]
sol.parameters
ans = 0.5000
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Linear Algebra 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by