MATlab code line doesnt run

I am having the following MATlab code and keep running into the error of my xdot(i) line.
Error: Check for missing argument or incorrect argument data type in
call to function 'R'.
Error in 360Second (line 14)
xdot(i)=[x(3);x(4);-u*x/R(i)^3;-u*y/R(i)^3];
can you please check if you see anything else wrong with it after I fix that error.
a= 7000;
e= 0.1;
time= (60*60*24);
u= 3.986*10^5;
dt=0.1;
t=(0:.1:86400);
Rp= a*(1-e);
Vp=sqrt(u*(2/Rp-1/a));
x=zeros(4);
x=[Rp;0;0;Vp];
i=1;
while t(i)<=86400
xdot(i)=[x(3);x(4);-u*x/R(i)^3;-u*y/R(i)^3];
x=x+xdot*dt;
i=i+1
R(i)=sqrt(x(1)^2+x(2)^2);
V(i)=sqrt(x(3)^2+x(4)^2);
E(i)=V(i)^2/2-u/R(i);
end
plot(R,t);
plot(V,t);
plot(E,t);
title('random'), xlabel('random'), ylabel('random');
grid on

7 comentarios

the cyclist
the cyclist el 31 de Mzo. de 2021
Please copy/paste the actual code, or upload it in an M-file.
Except for absolutely trivial errors that can be spotted immediately, images are useless, because we would need to type all that code ourselves, to debug it.
Oskar Kinat
Oskar Kinat el 31 de Mzo. de 2021
a= 7000;
e= 0.1;
time= (60*60*24);
u= 3.986*10^5;
dt=0.1;
t=(0:.1:86400);
Rp= a*(1-e);
Vp=sqrt(u*(2/Rp-1/a));
x=zeros(4);
x=[Rp;0;0;Vp];
i=1;
while t(i)<=86400
xdot(i)=[x(3);x(4);-u*x/R(i)^3;-u*y/R(i)^3];
x=x+xdot*dt;
i=i+1
R(i)=sqrt(x(1)^2+x(2)^2);
V(i)=sqrt(x(3)^2+x(4)^2);
E(i)=V(i)^2/2-u/R(i);
end
plot(R,t);
plot(V,t);
plot(E,t);
title('random'), xlabel('random'), ylabel('random');
grid on
the cyclist
the cyclist el 31 de Mzo. de 2021
When I run your code, I get the error message:
Unrecognized function or variable 'R'
Have you defined R? Can you share that function?
Oskar Kinat
Oskar Kinat el 31 de Mzo. de 2021
Editada: per isakson el 31 de Mzo. de 2021
I moved R(i) above the xdot and added R=[Rp,o]
R= [Rp,0];
Rp= a*(1-e);
Vp=sqrt(u*(2/Rp-1/a));
x=zeros(4);
x=[Rp;0;0;Vp];
i=1;
while t(i)<=86400
R(i)=sqrt(x(1)^2+x(2)^2);
xdot(i)=[x(3);x(4);-u*x/R(i)^3;-u*y/R(i)^3];
x=x+xdot*dt;
i=i+1
V(i)=sqrt(x(3)^2+x(4)^2);
E(i)=V(i)^2/2-u/R(i);
end
Oskar Kinat
Oskar Kinat el 31 de Mzo. de 2021
Now I get a different error
"
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
"
the cyclist
the cyclist el 31 de Mzo. de 2021
You don't define the variable y anywhere in this code, so it does not run for me.
Can you please upload the complete code, and first verify that it gives the error you are getting.
Oskar Kinat
Oskar Kinat el 31 de Mzo. de 2021
I' am sorry I just added the y=0 I believe thats right.
a= 7000;
e= 0.1;
time= (60*60*24);
u= 3.986*10^5;
dt=0.1;
t=(0:.1:86400);
y=0;
R= [Rp,0];
Rp= a*(1-e);
Vp=sqrt(u*(2/Rp-1/a));
x=zeros(4);
x=[Rp;0;0;Vp];
i=1;
while t(i)<=86400
R(i)=sqrt(x(1)^2+x(2)^2);
xdot(i)=[x(3);x(4);-u*x/R(i)^3;-u*y/R(i)^3];
x=x+xdot*dt;
i=i+1
V(i)=sqrt(x(3)^2+x(4)^2);
E(i)=V(i)^2/2-u/R(i);
end
plot(R,t);
plot(V,t);
plot(E,t);
title('random'), xlabel('random'), ylabel('random');
grid on

Iniciar sesión para comentar.

Respuestas (1)

Neeraj Rajpurohit
Neeraj Rajpurohit el 7 de Abr. de 2021

0 votos

DISCLAIMER: These are my own views and in no way depict those of MathWorks.
Greetings,
I see that you are accessing arrays R, V and E, without initialization in the line .To make the given script work, you will have to do the initialization of these variables. Also, to access the variables with indices, you will have to preallocate the arrays with appropriate sizes.

Categorías

Más información sobre Graphics Performance en Centro de ayuda y File Exchange.

Productos

Versión

R2020b

Preguntada:

el 31 de Mzo. de 2021

Respondida:

el 7 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by