What is the need for a nested function?

조회 수: 3 (최근 30일)
Avinash Gupta
Avinash Gupta 2022년 11월 22일
댓글: Avinash Gupta 2022년 11월 22일
Hello. This is a code for solving van der Pol equation using ode45. Its available on 12000.org. I hope posting it here is not violating any community guidelines.
Problem: I do not quite see the need for the 'function nma_project_511()'. Its neither taking any input nor giving any outpit. However, running the code without it throws an error regarding c and k being unrecognized. Kindly advise.
Website link: https://12000.org/my_notes/vanderpol/using_ODE45_to_solve_vanderpol/index.htm
function nma_project_511()
% This function solves the Van Der Pol nonlinear ode
% numerically using Matlab's ode45.
% % Final project for 511, CSUF, spring 2009. Instructor: Dr Oh, Sang June
% % Written by : Nasser M. Abbasi
% Van Der Pol ode is x''(t) - c (1-x^2(t)) x'(t) + k x(t) = 0
t = 0:0.001:100; % time scale
c = 1; k = 1;
x0 = 10; v0 = 3;
[t,x] = ode45( @rhs, t, [x0,v0]);
subplot(2,1,1);
plot(t,x(:,1));
xlabel('t'); ylabel('x(t)');
title(sprintf('Van Der Pol, position vs, time, c=%3.2f, k=%3.2f',c,k));
subplot(2,1,2);
plot(x(:,1),x(:,2));
xlabel('x(t)'); ylabel('v(t)');
hold on;
plot(x0,v0,'*r','MarkerSize',10);
title(sprintf('Phase portait showing limit cycle. x(0)=%3.2f, v(0)=%3.2f',x0,v0));
axis equal
hold off;
function dxdt=rhs(t,x)
dxdt_1 = x(2);
dxdt_2 = c*(1-x(1)^2)*x(2)-k*x(1);
dxdt = [dxdt_1 ; dxdt_2];
end
end
  댓글 수: 3
Stephen23
Stephen23 2022년 11월 22일
"The function is used to define the ODE and use it as an input to ode45(), as it can be seen in the this command "
No, that is the function RHS(). The OP is asking about the function NMA_PROJECT_511().
Dyuman Joshi
Dyuman Joshi 2022년 11월 22일
You are correct, Stephen. I didn't read through properly, my bad.

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

채택된 답변

Stephen23
Stephen23 2022년 11월 22일
편집: Stephen23 2022년 11월 22일
"I do not quite see the need for the 'function nma_project_511()'"
There are two main ways to pass extra parameters to functions that are provided to ODE or optimization routines:
As you can see, using nested functions is one way of parameterizing a function. The author of that code chose to use a nested function (not required, but a design choice). A nested function requires a parent function. Ergo, the function NMA_PROJECT_511() is required to make the code work, otherwise RHS() would not be a nested function.
PS: If you have a background in some other languages then you might be used to variables magically being accessible in other workspaces based on very broad and generous scoping rules. MATLAB is not other languages: unless you specifically do something to make it happen (e.g. pass arguments or write a nested function) variables are not accessible in other functions' workspaces.
  댓글 수: 1
Avinash Gupta
Avinash Gupta 2022년 11월 22일
Thanks for the quick response. I didn't know about these concepts. I will read through them.
Cheers!!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by