When I run my code, I keep getting this error: "The following error occurred converting from sym to double: Unable to convert expression into double array."

1 Ansicht (letzte 30 Tage)
clc;
clear;
close;
x=linspace(0,1,100);
solinit = bvpinit(x,[2 4]);
y = bvp4c(@ode2, @bc2, solinit);% ВЫЗЫВАЕМ СОЛВЕР BVP4C:
y=deval(y,x);
plot(x,y(1,:));
grid on
legend('y(t)');
xlabel('t');
function dy = ode2(~, y)%y''+y'=1+int^1_0(y'*x)dx;y(0) = 2;y(1) = 4;
dy=zeros(2,1); % создает нулевой вектор-столбец
dy(1)=y(2);
syms y(x) x
dy(2)=1-y(2)+int(y(2)*x,'x',[0 1]);
end
function res = bc2(ya, yb)
res = [ya(1)-2
yb(1)-4];
end
Error in IDTboundary2>ode2 (line 16)
dy(2)=1-y(2)+int(y(2)*x,'x',[0 1]);
Error in bvparguments (line 105)
testODE = ode(x1,y1,odeExtras{:});
Error in bvp4c (line 130)
bvparguments(solver_name,ode,bc,solinit,options,varargin);
Error in IDTboundary2 (line 6)
y = bvp4c(@ode2, @bc2, solinit);% ВЫЗЫВАЕМ СОЛВЕР BVP4C:

Antworten (1)

Torsten
Torsten am 13 Mai 2023
Bearbeitet: Torsten am 13 Mai 2023
So you have a constant source term
int^1_0(y'*x)dx
in all grid points - independent of the position x in the interval [0 1] ?
Then use
x = linspace(0,1,100);
solinit = bvpinit(x,[2 4]);
source = 0;
tol = 1e-3;
error = 1.0;
while error > tol
sol = bvp4c(@(x,y)ode2(x,y,source),@bc2,solinit);% ВЫЗЫВАЕМ СОЛВЕР BVP4C:
source_new = trapz(sol.x,sol.x.*sol.y(2,:));
error = abs(source_new - source);
source = source_new;
end
plot(sol.x,sol.y(1,:));
grid on
legend('y(t)');
xlabel('t');
function dy = ode2(x,y,source)%y''+y'=1+int^1_0(y'*x)dx;y(0) = 2;y(1) = 4;
dy = zeros(2,1); % создает нулевой вектор-столбец
dy(1)=y(2);
dy(2)=1-y(2)+source;
end
function res = bc2(ya,yb)
res = [ya(1)-2
yb(1)-4];
end

Kategorien

Mehr zu Argument Definitions finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by