3 views (last 30 days)

Show older comments

I am getting this error where I am sure the size of the left-side = the right side in this code, what else can be causing this problem?

Unable to perform assignment because the size of the left side is 8-by-1 and the size of the right side is 8-by-1001.

Error in RK4

U_RK(:,j) = U_T(:,j)-(dt./dx)*a(k)*(Res);

function [ U_RK ] = RK4( U_T,Res, dx, dt, N,k )

U_RK = U_T;

a = zeros(4,1);

a(1) = 0.25;

a(2) = 1.0/3.0;

a(3) = 0.5;

a(4) = 1.0;

for j = 2:N-2

U_RK(:,j) = U_T(:,j)-(dt./dx)*a(k)*(Res); % error here

end

end

But U_Rk(:,j) is initailly set to = U_T, and is initizalize this way:

L = 1; %domain length

x = 0:dx:L;

N = length(x);

U_T= zeros(8,N);

So how is the LHS does not equal the RHS??

KSSV
on 6 May 2021

The error is clear, you are trying to save more number of elements in the initilaized matrix.

Example:

A = zeros(3,3) ; % initialize A to 3x3 zeros matrix, to save data later

A(:,1) = rand(1,3) ; % no error, because column should have three elements and we have three here

A(:,2) = rand(1,5) ; % error, because column should have three elements, but you are saving five elements, This is not allowed

So, similiarly in your case, U_RK is a array with 8 rows, where you should save 8*1 array; but the RHS is giving you an array of size 8*1001.

Check the dimensions of your code and then initialize the matrix properly.

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

Start Hunting!