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

3 views (last 30 days)
max french on 14 Jan 2020
Commented: Star Strider on 14 Jan 2020
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 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.

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.

Show 1 older comment
Star Strider on 14 Jan 2020
Steven Lord also caught the problem with ‘X’ that I overlooked.
Also remember that ‘X’ is (in your code) a 2-element vector, so in the ‘dydt(2)’ assignment, you need to choose oone element of ‘X’. I chose ‘X(1)’ here, change that if it is not what you want:
function dydt = Odefunc(Time,X)
dydt = zeros(2,1);
G1 = [6,4,2.5,1.5];
% G = rot90(rot90(rot90(G1)));
G = G1(1);
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(1).^2)+(T2*X(1))+T3)-((B1*((V1*(X(1)-V2))./G).^B2)./G))./(B2*G+(B3./G));
end
and the ode45 call needs to be changed as well:
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(@Odefunc, tspan, init);% Odesolver();
figure
plot(Time,X)
grid
I added the plot call so I could see what the function was doing.
This runs without error.
Make appropriate changes to get it to do what you want. I will help as much as I can.
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 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)’.