"In an assignment A(I) = B, the number of elements in B and I must be the same" and cant work out why
Info
This question is closed. Reopen it to edit or answer.
Show older comments
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
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
on 12 Mar 2014
Marta Salas
on 12 Mar 2014
What's the purpose for the function fun?
Simon Williams
on 12 Mar 2014
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!