(Biginner) Code does not work! Please. I need a hand.

1 visualizzazione (ultimi 30 giorni)
I learned some things about this. One, even defining rho and G outside the functions "dp" and " deriv_M" it seems that when MATLAB goes in these functions they do not recognize rho and G so I had to define them inside each function. I just do not know why!!?? Why MATLAB doesnt recognize theses constants?
I used the command global but it didn't work. ( I thought the problem was because these constants were defined just for function gas and then they werent global constants... (didnt work either)
What is the mistery behind it?
Second, I used debugger and this line ("[r1,p] = ode45(@P, Rrange,p_init);") doesnt work. It says variable m not defined. Again, I think that when MATLAB goes inside that function all the variables already estabilished are somehow gone! How do I fix it? How do I make "m" visible to the function dp ??? (That makes the code not run)...
____________________________________________________
function gas
rho = 1;
G = 1;
% Radii for each value of mass
Rrange = [.003 , 10^3] ;
% M(r = 0) = 0 initial value
m0 = 0;
[r, m] = ode45(@M, Rrange,m0);
% P(r = Rmax) = 0
p_init = 0;
subplot(2,1,1)
plot(r,m)
[r1,p] = ode45(@P, Rrange,p_init);
subplot(2,1,1)
plot(r1,p)
function dp = P(r1,p)
G = 1;
rho = 1;
dp = -(rho*G.*m)./r1^2;
function deriv_M = M(r,m)
rho = 1;
G = 1;
deriv_M = 4*(pi*(r.^2))*rho;

Risposta accettata

per isakson
per isakson il 11 Mag 2014
  2 Commenti
Douglas Alves
Douglas Alves il 11 Mag 2014
Thank you! That helped me a lot. Now I managed to make the code works. It turned out there's another problem... lol I'm trying to figure that out still..
Image Analyst
Image Analyst il 11 Mag 2014
Well go ahead and officially "Accept" his answer for this question to close it out and give him credit. Then, you can post another question on the other problem later if you need to.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Programming in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by