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 (최근 30일)
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:

답변 (1개)

Torsten
Torsten 2023년 5월 13일
편집: Torsten 2023년 5월 13일
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

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by