Info

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

"In an assignment A(I) = B, the number of elements in B and I must be the same" and cant work out why

1 view (last 30 days)
I understand that it means that the matrices involved must be the same size. Apologies if i'm being stupid! Here is the main code:
function [] = BH();
mp=0.75;
ms=0.25;
mbh=1e6;
f0=-1.982;
f(1)=f0;
t0=(sqrt(6)*tan(f0/2)*(3+(tan(f0/2)^2)));
t(1)=t0;
R=((6*(mbh.^(1/3)))/(1+cos(f0)));
fdot=(sqrt(6)/36)*(1+cos(f0)).^2;
Rdot=((6*(mbh.^(1/3)))/((1+cos(f0)).^2))*fdot*sin(f0);
%initial conditions
xBH=0;
yBH=0;
vxBH=0;
vyBH=0;
xp(1)=(6*mbh.^(1/3)*cos(f0)/(1+cos(f0)));
yp(1)=(6*mbh.^(1/3)*sin(f0)/(1+cos(f0)))-ms;
vxp(1)=((Rdot*cos(f0))-(R*fdot*sin(f0)))+ms;
vyp(1)=((Rdot*sin(f0))+(R*fdot*cos(f0)));
xs(1)=(6*mbh.^(1/3)*cos(f0)/(1+cos(f0)));
ys(1)=(6*mbh.^(1/3)*sin(f0)/(1+cos(f0)))+mp;
vxs(1)=((Rdot*cos(f0))-(R*fdot*sin(f0)))-mp;
vys(1)=((Rdot*sin(f0))+(R*fdot*cos(f0)));
h=0.5;
nsteps=(f0)/-h;
for i=1:nsteps;
k1=h.*fun(f(i),xp(i));
k2=h.*fun(f(i)+k1/2,xp(i)+k1/2);
k3=h.*fun(f(i)+k2/2,xp(i)+k2/2);
k4=h.*fun(f(i)+k3,xp(i)+k3);
f(i+1)=f(i)-(k1./6)-(k2./3)-(k3./3)-(k4./6);
xp(i+1)=xp(i)+(k1(1,2)./6)+(k2(1,2)./3)+(k3(1,2)./3)+(k4(1,2)./6)
t(i+1)=t(i)+h;
end
It uses a separate function called "fun":
function a = fun(f,xp)
a(1) = f;
a(2) = xp;
this returns the error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in fun (line 2) a(1) = f;
Error in BH (line 30) k2=h.*fun(f(i)+k1/2,xp(i)+k1/2);
I don't see why though as there are only two variables specified in the "k1" line of the for loop. Any help much appriciated!

Answers (1)

Marta Salas
Marta Salas on 12 Mar 2014
Edited: Marta Salas on 12 Mar 2014
k1 is the output of fun so it's an array, it has 2 values. So, when you call
fun(f(i)+k1/2,xp(i)+k1/2)
you are trying to assign an array k1 (dim=1x2) to a(1) (dim=1x1) . The dimension doesn't agree.
  3 Comments
Simon Williams
Simon Williams on 12 Mar 2014
It's ok now. Its working now oddly, I don't know what I changed!
If you are interested however I am writing a simulation of a three body system, two of which are a binary star system and the third a supermassive black hole. The binary is to pass by the black hole on a parabolic orbit.
This is now the working code:
function [] = BH();
mp=0.75;
ms=0.25;
mbh=1e6;
f0=-1.982;
f(1)=f0;
t0=(sqrt(6)*tan(f0/2)*(3+(tan(f0/2)^2)));
t(1)=t0;
R=((6*(mbh.^(1/3)))/(1+cos(f0)));
fdot=(sqrt(6)/36)*(1+cos(f0)).^2;
Rdot=((6*(mbh.^(1/3)))/((1+cos(f0)).^2))*fdot*sin(f0);
%initial conditions
xBH=0;
yBH=0;
vxBH=0;
vyBH=0;
xp(1)=(6*mbh.^(1/3)*cos(f0)/(1+cos(f0)));
yp(1)=(6*mbh.^(1/3)*sin(f0)/(1+cos(f0)))-ms;
vxp(1)=((Rdot*cos(f0))-(R*fdot*sin(f0)))+ms;
vyp(1)=((Rdot*sin(f0))+(R*fdot*cos(f0)));
xs(1)=(6*mbh.^(1/3)*cos(f0)/(1+cos(f0)));
ys(1)=(6*mbh.^(1/3)*sin(f0)/(1+cos(f0)))+mp;
vxs(1)=((Rdot*cos(f0))-(R*fdot*sin(f0)))-mp;
vys(1)=((Rdot*sin(f0))+(R*fdot*cos(f0)));
h=0.1;
nsteps=(f0)/-h;
for i=1:nsteps
k1=h.*fun(f(i),xp(i));
k2=h.*fun(f(i)+k1(1,1)/2,xp(i)+k1(1,2)/2);
k3=h.*fun(f(i)+k2(1,1)/2,xp(i)+k2(1,2)/2);
k4=h.*fun(f(i)+k3(1,1),xp(i)+k3(1,2));
t(i+1)=t(i)+h;
f(i+1)=f(i)-(k1(1,1)./6)-(k2(1,1)./3)-(k3(1,1)./3)-(k4(1,1)./6);
xp(i+1)=xp(i)+(k1(1,2)./6)+(k2(1,2)./3)+(k3(1,2)./3)+(k4(1,2)./6)
end
And the function:
function xi=func(f,xp)
xi(1)=f; %outputting new xp
xi(2)=xp; %outputting new f

This question is closed.

Community Treasure Hunt

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

Start Hunting!