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

조회 수: 1 (최근 30일)
Douglas Alves
Douglas Alves 2014년 5월 11일
댓글: Image Analyst 2014년 5월 11일
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;

채택된 답변

per isakson
per isakson 2014년 5월 11일
  댓글 수: 2
Douglas Alves
Douglas Alves 2014년 5월 11일
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 2014년 5월 11일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by