''Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-100'' error when updating my vector W

1 view (last 30 days)
Kindly help me out. I'm getting this error ''Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-100'' when updating my vector W.
function [y,t] = RK4(y_old,t_old,rhs,h)
% integrates the ODE specified by function rhs
% using the RK4 method
t(1) = t_old; %start time is the previous value
y(1) = y_old; %initial position of sphere is previous value
%pre-allocate memory
W = zeros(1,100);
X = zeros(1,100);
F1 = @(t,r)X;
F2 = @(t,r)rhs;
i=1;
for i=1:100 %perform 100 RK4 itarations for a single time step dt
W1 = F1(t(i),y(i));
X1 = F2(t(i),y(i));
W2 = F1(t(i)+0.5*h,y(i)+0.5*h*W1);
X2 = F2(t(i)+0.5*h,y(i)+0.5*h*X1);
W3 = F1((t(i)+0.5*h),(y(i)+0.5*h*W2));
X3 = F2((t(i)+0.5*h),(y(i)+0.5*h*X2));
W4 = F1((t(i)+h),(y(i)+W3*h));
X4 = F2((t(i)+h),(y(i)+X3*h));
W(1,i+1) = W(1,i)+ (1/6)*(W1+2*W2+2*W3+W4)*h; %update value of W
X(i+1) = X(i) + (1/6)*(X1+2*X2+2*X3+X4)*h; %update value of W
end

Accepted Answer

the cyclist
the cyclist on 24 Apr 2022
The way you have defined
F1 = @(t,r)X;
the result of this line
W1 = F1(t(i),y(i));
is going to be W1 = zeros(1,100);
Note that the actual definition of F1 does not depend on t or r at all. F1 is just going to be X.
I can't tell what you actually intended.
  2 Comments
Eric Muia
Eric Muia on 24 Apr 2022
I've updated the code here, the error persists. kindly help me out.
function [q,t] = RK4(q_old,t_old,rhs,h)
% integrates the ODE specified by function rhs
% using the RK4 method
%initialize vector q which will later contain the solutions
q = zeros(2,100) ;
t(1) = t_old; %start time is the previous value t-old
y(1) = q_old; %initial value of the solution
% rhs(which is a second order ODE)
% To convert RHS into a system of first order ODEs by introducing new holders (W and X)
DW = diff(rhs,t); % evaluate the first derivative
DX = diff(rhs,t,2); % evaluate the second derivative
%pre-allocate memory
W = zeros(1,100);
X = zeros(1,100);
F1 = @(t,rhs)X;
F2 = @(t,rhs)W;
for i=1:100 %perform 100 RK4 itarations for a single time step h
W1 = F1(t(i),y(i));
X1 = F2(t(i),y(i));
W2 = F1(t(i)+0.5*h,y(i)+0.5*h*W1);
X2 = F2(t(i)+0.5*h,y(i)+0.5*h*X1);
W3 = F1((t(i)+0.5*h),(y(i)+0.5*h*W2));
X3 = F2((t(i)+0.5*h),(y(i)+0.5*h*X2));
W4 = F1((t(i)+h),(y(i)+W3*h));
X4 = F2((t(i)+h),(y(i)+X3*h));
W(1,i+1) = W(1,i)+ (1/6)*(W1+2*W2+2*W3+W4)*h; %update value of W
X(i+1) = X(i) + (1/6)*(X1+2*X2+2*X3+X4)*h; %update value of W
end
q = [W,X];
the cyclist
the cyclist on 24 Apr 2022
I suggest you learn to use the debugger, so that you can stop your code at any point, and inspect the values of your variables at that point.
The change you made in your code did not change the fact that your function F1 (and the choice of X)
X = zeros(1,100);
F1 = @(t,r)X;
ignores the inputs, and simply outputs the vector X, which is a vector of zeros. For example:
F1(300,400)
ans = 1×100
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
or
F1(8987087087,31415892)
ans = 1×100
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!