3 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

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.

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.

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.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/500283-ode45-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-eleme#comment_786068

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/500283-ode45-unable-to-perform-assignment-because-the-left-and-right-sides-have-a-different-number-of-eleme#comment_786068

Sign in to comment.