Error using vertcat Dimensions of arrays being concatenated are not consistent.

6 次查看(过去 30 天)
Hello. When I start the program, I get an error about using vertcat, I don’t understand why, I tried to find information and fix it, but apparently I’m missing something. What can I do to solve the problem?
function f=M86()
n = 1;b=1; k = -4; a = 1; h = 3; p = (1/4)*pi; y(1)=0;y(2)=1;x=2;
f=[y(2);y(2)+2.*n.*(1+a.*y.^3).*y(1)+k.^2.*(y(1)+b.*y(1).^3)-h.*sin(p.*x)];
clc
disp('Значения параметров: n = 1,b=2, k = -4, a = 1, h = 3,b =2, p = (1/4)*Pi')
disp('Решение задачи Коши для уравнения')
[~,y] = ode45('f',[0,1],[0,0],1,2,-4,1,3,2,(1./4).*pi);
plot(y(:,1),y(:,2))
tic,[~,~]=ode45('M86',[-1,1],[0,0],1,2,-4,1,3,2,(1./4).*pi);toc
grid on
end
  3 个评论
Walter Roberson
Walter Roberson 2023-11-24
Note by the way that using a character vector (or string scalar) as the name of the ode function is recommended against. You should use a function handle instead.
When you use a character vector (or string scalar) then the function named must have its own .m file (or be a .mdl or .slx or .p). In particular, it cannot be the name of a function that is in the same file as the ode45* call.
Walter Roberson
Walter Roberson 2023-11-24
disp('Значения параметров: n = 1,b=2, k = -4, a = 1, h = 3,b =2, p = (1/4)*Pi')
Значения параметров: n = 1,b=2, k = -4, a = 1, h = 3,b =2, p = (1/4)*Pi
disp('Решение задачи Коши для уравнения')
Решение задачи Коши для уравнения
[~,y] = ode45(@f,[0,1],[0,0],1,2,-4,1,3,2,(1./4).*pi);
Unrecognized function or variable 'f'.

Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.

Error in ode45 (line 104)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
plot(y(:,1),y(:,2))
tic,[~,~]=ode45(@M86,[-1,1],[0,0],1,2,-4,1,3,2,(1./4).*pi);toc
grid on
function f=M86()
n = 1;b=1; k = -4; a = 1; h = 3; p = (1/4)*pi; y(1)=0;y(2)=1;x=2;
f=[y(2);y(2)+2.*n.*(1+a.*y.^3).*y(1)+k.^2.*(y(1)+b.*y(1).^3)-h.*sin(p.*x)];
end

请先登录,再进行评论。

采纳的回答

Torsten
Torsten 2023-11-25
编辑:Torsten 2023-11-25
Maybe you mean something like this.
I don't know whether it must read y(1) or y(2) at the place marked in bold letter in the next line ; I assumed y should be y(1).
f=@(t,y)[y(2);y(2)+2.*n.*(1+a.*y.^3).*y(1)+k.^2.*(y(1)+b.*y(1).^3)-h.*sin(p.*x)];
M86()
Значения параметров: n = 1,b=1, k = -4, a = 1, h = 3, p = (1/4)*Pi Решение задачи Коши для уравнения
function M86
n = 1;b=1; k = -4; a = 1; h = 3; p = (1/4)*pi; y(1)=0;y(2)=1;x=2;
f=@(t,y)[y(2);y(2)+2.*n.*(1+a.*y(1).^3).*y(1)+k.^2.*(y(1)+b.*y(1).^3)-h.*sin(p.*x)];
disp('Значения параметров: n = 1,b=1, k = -4, a = 1, h = 3, p = (1/4)*Pi')
disp('Решение задачи Коши для уравнения')
[~,y] = ode45(f,[0,1],[0,0]);
plot(y(:,1),y(:,2))
end

更多回答(0 个)

类别

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

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by