How can variables be passed to a function

조회 수: 2 (최근 30일)
Jared
Jared 2013년 11월 20일
댓글: Azzi Abdelmalek 2013년 11월 20일
If I define a function in F.m file with the following:
function zp=F(x,z)
zp=zeros(2,1);
zp(1)=z(2);
zp(2)=a*sqrt(1+z(2)^2)+kx;
That is a function I want to evaluate by using:
[x,z]=ode45('F',[x0,xf],[z10,z20]);
I'm not sure how I can pass a and kx to that function before it is evaluated.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 11월 20일
function zp=F(x,z,a,kx)
zp=zeros(2,1);
zp(1)=z(2);
zp(2)=a*sqrt(1+z(2)^2)+kx;
% ---------------------------------------------
[x,z]=ode45(@(x,z),F(x,z,a,kx),[x0 xf],[z10;z20]);
  댓글 수: 2
Jared
Jared 2013년 11월 20일
I'm not quite sure on the syntax but it appears that there is something a bit off, possibly the comma in between (x,z) and F.
Azzi Abdelmalek
Azzi Abdelmalek 2013년 11월 20일
Yes, remove a comma
[x,z]=ode45(@(x,z) F(x,z,a,kx),[x0 xf],[z10;z20]);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by