Doubt in declaring a function

조회 수: 1 (최근 30일)
Pankaj
Pankaj 2016년 1월 28일
댓글: Walter Roberson 2016년 1월 28일
I have a doubt regarding declaration of a function, kindly conside the following code
fun = @GVF
sqrt_estimt = fminsearch(@(theta)ODE_fit(fun,t,y,Q,theta(1),theta(2), iniVal), theta0);
..
function err = ODE_fit(fun, exp_t, exp_y, Q, theta(1), theta(2), iniVal)
% exp_y = Experimental observation at time exp_t
[t,y] = ode45(@(t,X)fun(t,X,n, S0, Q, theta(1),theta(2)),exp_t,iniVal);
err = sum(sum((y-exp_y).^2)); % compute error between experimental y and fitted y
end
..
function dy = GVF(x, y, Q, T, g)
% T = theta(1)
% g = theta(2);
A = y*T;
V = Q/A;
P = T+2*y;
R = A/P;
Sf = (.14*V)^2/(R^(4/3));
Fr = V/sqrt(g*y);
dy = (Sf)/(1-Fr^2);
end
..
The above does not work, but the following does: Is there a way to make the above way function? Thanks
sqrt_estimt = fminsearch(@(theta)ODE_fit(fun, t, y, Q, theta, iniVal), theta0);
..
function err = ODE_fit(fun, exp_t, exp_y, Q, theta, iniVal)
% exp_y = Experimental observation at time exp_t
[t,y] = ode45(@(t,X)fun(t,X,n, S0, Q, theta(1),theta(2)),exp_t,iniVal);
err = sum(sum((y-exp_y).^2)); % compute error between experimental y and fitted y
end
..
function dy = GVF(x, y, Q, T, g)
T = theta(1)
g = theta(2);
A = y*T;
V = Q/A;
P = T+2*y;
R = A/P;
Sf = (.14*V)^2/(R^(4/3));
Fr = V/sqrt(g*y);
dy = (Sf)/(1-Fr^2);
end

채택된 답변

Walter Roberson
Walter Roberson 2016년 1월 28일
No, there is not. You cannot name an element of a matrix in a function header. You can use two different variables though.
function err = ODE_fit(fun, exp_t, exp_y, Q, theta1, theta2, iniVal)
% exp_y = Experimental observation at time exp_t
[t,y] = ode45(@(t,X)fun(t,X,n, S0, Q, theta1,theta2),exp_t,iniVal);
err = sum(sum((y-exp_y).^2)); % compute error between experimental y and fitted y
end
  댓글 수: 2
Pankaj
Pankaj 2016년 1월 28일
Thank you Walter, you cleared my doubt.
Walter Roberson
Walter Roberson 2016년 1월 28일
Please Accept the answer to indicate you are finished with the Question.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by