Info

This question is closed. Reopen it to edit or answer.

I don't why I get error when i run this code In an assignment A(I) = B, the number of elements in B and I must be the same. " Error in prob5 (line 26) v(i+1)=vi+​(1/6)*(k11​+2*k21+2*k​31+k41);

1 view (last 30 days)
%Script that demonstrates the Runge-Kutta integration for a first order problem
%the problem to be solved is:
%vprime= @(xi,ti) 0.5*ui-1.5*vi
%uprime=2*ui-(2/3)*u^.3-vi
h=0.1; %h is the time step.
u0=0;
v0=0;
ti=t0;
to=0;
vi=v0;
tf=10;
ui=u0;
n=tf-t0/tf;
%iterate
for i=0:n
vprime=@(vi,ui,ti) 0.5*ui-1.5*vi;
uprime=@(vi,ui,ti)2*ui-(2/3)*u.^3-vi;
k11=h*vprime(vi,ui,ti);
k12=h*uprime(vi,ui,ti);
k21=h*vprime(vi+(1/2)*k11,ui+(1/2)*k12,ti+h/2);
k22=h*uprime(vi+(1/2)*k11,ui+(1/2)*k12,ti+(1/2)*h);
k31=h*vprime(vi+(1/2)*k21,ui+(1/2)*k22,ti+(1/2)*h);
k32=h*uprime(vi+(1/2)*k21,ui+(1/2)*k22,ti+(1/2)*h);
k41=h*vprime(vi+k31,ui+k32,ti+h);
k42=h*uprime(vi+k31,ui+k32,ti+h);
v(i+1)=vi+(1/6)*(k11+2*k21+2*k31+k41);
u(i+1)=ui+(1/6)*(k12+2*k22+2*k32+k42);
end
v(i+1)
u(i+1)

Answers (1)

Image Analyst
Image Analyst on 10 Oct 2012
Set a breakpoint on the line:
v(i+1)=vi+(1/6)*(k11+2*k21+2*k31+k41);
You'll see one of the variables vi or k11, k21, k31, or k41 is not a single number. You can't set one element of an array equal to an array (unless it's a special kind of array called a cell array). For example v(2) can't be [1 2 3 4 5] - that's too many numbers, it has to be just one number, like 42.

Community Treasure Hunt

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

Start Hunting!