can someone help with this error please

조회 수: 3 (최근 30일)
E.Wolf
E.Wolf 2023년 4월 30일
편집: Torsten 2023년 4월 30일
% Define the differential equation
f = @(t,x) x.^2 - t;
% Define the time interval and initial conditions
tspan = [0,2];
a1 = 0;
a2 = 0.75;
% Solve the differential equation using ode45
[t1,x1] = ode45(f,tspan,a1);
[t2,x2] = ode45(f,tspan,a2);
% Plot the solutions
hold on
plot(t1,x1,'b','LineWidth',2)
plot(t2,x2,'r--','LineWidth',2)
xlabel('t')
ylabel('x')
legend('a=0','a=0.75')
% Plot the distance between the trajectories
figure
plot(t1,abs(x1-x2),'k','LineWidth',2)
Arrays have incompatible sizes for this operation.
xlabel('t')
ylabel('|x1(t) - x2(t)|')

답변 (2개)

Torsten
Torsten 2023년 4월 30일
편집: Torsten 2023년 4월 30일
Instead of
tspan = [0,2]
use
tspan = linspace(0,2,100)
This fixes the number of output times to 100 for both calls to ode45.

Image Analyst
Image Analyst 2023년 4월 30일
x1 has 41 elements while x2 has 45 elements, so how can you subtract them?

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by