Clear Filters
Clear Filters

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

6 views (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 24 Nov 2023
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 on 24 Nov 2023
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

Sign in to comment.

Accepted Answer

Torsten
Torsten on 25 Nov 2023
Edited: Torsten on 25 Nov 2023
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

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!