필터 지우기
필터 지우기

Passing Functions and m-files to functions

조회 수: 2 (최근 30일)
Linford Briant
Linford Briant 2013년 2월 12일
Hi Mathworks,
I have an m-file which codes up a system of differential equations;
9D_system
This has various parameters in it, and I have a few parameter schemes for specifying the values to these parameters;
P1.m, P2.m, P3.m
I want to be able to specify which parameter scheme I endow the system with, and then solve this system of differential equations with ode15s.
How do I do this?
Moreover, the parameter schemes
P1.m, P2.m, P3.m
each call functions (some of the parameters are functions of other parameters, and so must be calculated by these called functions). Thus the ode will have a series of nested functions being called!!
Any help on this would be greatly appreciated. :)
Linford
  댓글 수: 1
José-Luis
José-Luis 2013년 2월 12일
Pass them as a string and evaluate inside your function.

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

채택된 답변

Sean de Wolski
Sean de Wolski 2013년 2월 12일
@Linford, these are the ways we recommend passing extra parameters:
e.g:
ode45(@(t,y)myfun(t,y,@p1)
And
function x = myfun(t,y,pfun)
stuff = pfun();
x = do_things_with(stuff,t,y)
  댓글 수: 1
Jan
Jan 2013년 2월 12일
편집: Jan 2013년 2월 12일
Or add the parameters as additional arguments to the anoymous function directly. See also: http://www.mathworks.de/matlabcentral/answers/1971

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

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 2월 12일

José-Luis
José-Luis 2013년 2월 12일
myStr = 'P1.m'
function [result] = myFun(someArg, myStr)
[str str] = fileparts(myStr);
run(str); %calling your script
%Do your thing
  댓글 수: 1
Linford Briant
Linford Briant 2013년 2월 12일
편집: Linford Briant 2013년 2월 12일
Hi Jose-Luis,
Thanks for this; it works a treat for my parameter functions, but it isn't working for my ODE files. For example, the following is the first 4 lines of a file making my ODE file.
function xprime = 9D_system(t,x,myStr,L)
[str str] = fileparts(myStr);
run(str); %calling your script
%Do your thing
When I call this in MATLAB I get the error:
[t,x] = ode15s(@(t,x) 9D_system(t,x,myStr,0.001),[0 2e6],x0);
??? Input argument "myStr" is undefined.
Even though I have previously defined
myStr='Parameter1.m'

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by