필터 지우기
필터 지우기

Function definitions are not permitted in this context??

조회 수: 4 (최근 30일)
Michael
Michael 2013년 9월 8일
I have looked at other posts regarding this subject but nothing they suggested seemed to work for me. I seems that MATLAB doesn't like it when you declare variables prior to the function definition so as you can see in my code that is not the case. I am not sure why it's not working. It seems like it should be pretty simple. I am a mere novice with MATLAB so please go easy on me.
function dydt = Problem4(t,y)
dydt = y*t^3-1.5*y;
[t,y] = ode45(@Problem4, [0, 2], 1);
plot(t,y)
  댓글 수: 3
Michael
Michael 2013년 9월 8일
It should be there now but if not:
function dydt = Problem4(t,y)
dydt = y*t^3-1.5*y;
[t,y] = ode45(@Problem4, [0, 2], 1);
plot(t,y)
Michael
Michael 2013년 9월 8일
편집: Azzi Abdelmalek 2013년 9월 8일
If I run it directly from the editor window it seems to work however, it is telling me that my "y" argument is undefined. I thought I defined it in the line;
[t,y] = ode45(@Problem4, [0, 2], 1);
I thought the "1" is my "y" value. [0, 2] is the rang of "t" and 1 should "y."
Further help will be very appreciated, thanks in advance!

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

답변 (3개)

Image Analyst
Image Analyst 2013년 9월 8일
편집: Image Analyst 2013년 9월 8일
It seems like Problem4 is trying to call Problem4 inside of itself. Is it your intent to call this recursively? What function do you want to pass in to ode45?
Maybe you want this. I put all of this code below into a single m-file called Problem4.m and it worked:
function Problem4()
[t,y] = ode45(@ConstructEquation, [0, 2], 1);
plot(t,y)
function dydt = ConstructEquation(t,y)
dydt = y*t^3-1.5*y;

Azzi Abdelmalek
Azzi Abdelmalek 2013년 9월 8일
function dydt = Problem4(t,y)
dydt = y*t^3-1.5*y;
% save this function as Problem4.m
In another m file or in Matlab windows command run:
[t,y] = ode45(@Problem4, [0, 2], 1);
plot(t,y)
  댓글 수: 2
Michael
Michael 2013년 9월 8일
That worked. Is it because I have to "call" the function in the command window? I think I get it now...Thanks so much for your help.
Image Analyst
Image Analyst 2013년 9월 8일
No, you don't have to call the function in the command window. It can be all in one single m-file, like I showed.

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


Arthur
Arthur 2013년 9월 8일
You cannot define functions in the command window. They have to be in an m-file.
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2013년 9월 8일
He said to call a function in the command window, not to define it.
Arthur
Arthur 2013년 9월 8일
whoops my bad.

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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by