What does this declaration do?

조회 수: 4 (최근 30일)
TSAI SHIE JIUN
TSAI SHIE JIUN 2018년 2월 20일
댓글: TSAI SHIE JIUN 2018년 2월 21일
Hello I am new to matlab, just want to know what is the importance of this xval such that values are returned differently when using 3 different variable instead of two variable?
sample code:
function g=dVdx(xval,yval,cst)
g = 4*cst(1) - (4*yval)/cst(1)^2 ;
by input
>> dVdx(0,5E-6,[0.02]) I will get an answer of 0.0300.
however
function g=dVdx(xval,yval)
g = 4*xval - (4*yval)/xval^2 ;
Input
>> dVdx(5E-6,0.02)
I will get a return number of -3.2000e+09 .

채택된 답변

Geoff Hayes
Geoff Hayes 2018년 2월 20일
In your first example,
>> dVdx(0,5E-6,[0.02]) I
note how the first input 0 which is the xval is not used in your calculation (with yval equal to %E-6 and cst equal to 0.02)
g = 4*cst(1) - (4*yval)/cst(1)^2 ;
In your second example,
>> dVdx(5E-6,0.02)
xval is 5E-6 and yval is 0.02 for
g = 4*xval - (4*yval)/xval^2 ;
But the variables and their values aren't the same as your first example. If we substitute then we get
(1) g = 4*0.02 - (4*5E-6)/0.02^2 = 0.03
(2) g = 4*5E-6 - (4*0.02)/5E-6^2 = -3.2e+09
So you are using different values for each calculation (2 vs 3 inputs) and so will get different answers.
  댓글 수: 1
TSAI SHIE JIUN
TSAI SHIE JIUN 2018년 2월 21일
Thank you! Things seem much clearer to me now

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

추가 답변 (1개)

Star Strider
Star Strider 2018년 2월 20일
That appears to be an anonymous function to be used in one of the ODE solvers, such as ode45. The ODE solvers require that the first argument is the independent variable (here ‘xval’), and the second is the dependent variable (here ‘yval’). The third argument, ‘cst’, is an extra parameter.
The call to it in ode45 for example would be:
cst = 42; % Additional Parameter
tspan = [0 5]; % Time Range Or Vector
ic = 0; % Initial Condition
[x,y] = ode45(@(xval,yval) dVdx(xval,yval,cst), tspan, ic);
See the documentation on Parameterizing Functions (link) for details.

카테고리

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