ODE45 Unable to perform assignment because the left and right sides have a different number of element

2 views (last 30 days)
Code
On Call File
tspan = [0 50]; % time spane to solve over - s
init = [0 0]; % initial position and velocity of object (at time t=0)
[Time,X] = ode45(@(Time,X) Odefunc(Time), tspan, init);% Odesolver();
On File Odefunc.m
% Function solver
function dydt = Odefunc(Time,X)
dydt = zeros(2,1);
G1 = [6,4,2.5,1.5];
G = rot90(rot90(rot90(G1)));
Xmin = 1000;
Xmax = 8000;
Xstep = 1;
X = Xmin:Xstep:Xmax;
B1 = 0.06094;
B2 = 2;
B3 = 274;
T1 = -1.1905*(10^-5);
T2 = 0.09286;
T3 = 119;
V1 = 0.02618;
V2 = 1000;
dydt(1) = X(2);
dydt(2) = (((T1*X.^2)+(T2*X)+T3)-((B1*((V1*(X-V2))./G).^B2)./G))./(B2*G+(B3./G));
Error of Unable to perform assignment because the left and right sides have a different number of element, Tried everything i can think of to fix this, Weridly this formula works for normal calcuation for some reason it does not work in ODE
Version R2019b
  1 Comment
Steven Lord
Steven Lord on 14 Jan 2020
In addition to the problem identified by Star Strider, I have two comments. First, also related to G:
G1 = [6,4,2.5,1.5];
G = rot90(rot90(rot90(G1)));
This can be simplified.
G2 = G1.';
% To check compare G and G2
isequal(G, G2) % true
Second, X is the second input to your ODE function. However you overwrite it with another vector, defined in your function, before you use it to define the output of your ODE function.
X = Xmin:Xstep:Xmax;
So you're ignoring most of what ode45 passes into your ODE function. If you need to define and use this vector, give it a different name that doesn't conflict with the input to your ODE function.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 14 Jan 2020
The problem is ‘G’. Since it is a vector, all computations involving it are going to be arrays.
You can use it, however in every iteration, it needs to be a scalar. If it is time-dependent, see the documentation section on ODE With Time-Dependent Terms to understand how to interpolate it correctly.
  4 Comments
max french
max french on 14 Jan 2020
Thanks this is nearly doing excatly what i wanted, only thing is i need the output for all four 'G's so do i jsut repeat the Command 4 times or do i add another dydt?
sorry if these are simple questions reletively new to matlab
Star Strider
Star Strider on 14 Jan 2020
One option is to repeat the second differential equation once with each ‘G’. They will not interfere with each other because they are independent of each other in each iteration. This is a bit unusual, however it may do want you want.
The ODE function changes slightly to —
function dydt = Odefunc(Time,X)
% dydt = zeros(2,1);
G = [6,4,2.5,1.5];
% Xmin = 1000;
% Xmax = 8000;
% Xstep = 1;
% X = Xmin:Xstep:Xmax;
B1 = 0.06094;
B2 = 2;
B3 = 274;
T1 = -1.1905*(10^-5);
T2 = 0.09286;
T3 = 119;
V1 = 0.02618;
V2 = 1000;
dydt(1,:) = X(2);
for k = 2:5
dydt(k,:) = (((T1*X(1).^2)+(T2*X(1))+T3)-((B1*((V1*(X(1)-V2))./G(k-1)).^B2)./G(k-1)))./(B2*G(k-1)+(B3./G(k-1)));
end
end
and the call changes slightly to:
tspan = [0 50]; % time spane to solve over - s
init = zeros(1, 5); % initial position and velocity of object (at time t=0)
[Time,X] = ode45(@Odefunc, tspan, init);% Odesolver();
figure
plot(Time,X)
grid
The results for the individual ‘G’ values are ‘X(:,2:4)’.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!