(Biginner) Code does not work! Please. I need a hand.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Douglas Alves
am 11 Mai 2014
Kommentiert: Image Analyst
am 11 Mai 2014
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;
0 Kommentare
Akzeptierte Antwort
per isakson
am 11 Mai 2014
2 Kommentare
Image Analyst
am 11 Mai 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.
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!