parse error at .... :usage might be invalid matlab syntax
조회 수: 187 (최근 30일)
이전 댓글 표시
hi I'm using MATLAB R2016a and I give this error because my English is not so good please help me obviously. this is my code :
function y = g1(x)
if x>0
y=x;
elseif x<0
y=-x^2;
end
end
syms x y n
a_0=(1/pi)*int(g1,-pi,pi)
for n=1:10
a_n(n)=(1/pi)*int(g1*cos(n*x),-pi,pi);
b_n(n)=(1/pi)*int(g1*sin(n*x),-pi,pi);
end
a_n
b_n
f=a_0;
for n=1:10
f=f+a_n(n)*cos(n*x)+b_n(n)*sin(n*x);
end
ezplot(x,f)
When I run this it I receive this error :
g1(x)
Error: File: g1.m Line: 8 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of
the function "g1".)
답변 (2개)
Stephen23
2017년 1월 29일
편집: Stephen23
2017년 1월 29일
According to the MATLAB documentation Add Functions to Scripts "Functions in scripts are supported in R2016b or later".
Your version of MATLAB does not support mixing functions and scripts.
And even if your version did support functions in scripts, you did not read the documentation which states clearly that all local functions need to: "all appear after the rest of the script code". So you cannot put a function before your script code.
You might like to start using the MATLAB documentation. It tell us how to use MATLAB, and what to expect MATLAB to do.
Simplest solution: move your function to its own Mfile, named "g1.m".
댓글 수: 5
Guillaume
2017년 1월 30일
@f4r3in,
We've already been through the exact same issue in your previous question. That you keep making the same mistake is disheartening.
Star Strider
2017년 1월 29일
Save your function to a function file named ‘g1.m’.
I would not use the Symbolic Math Toolbox, since it is not efficient for recursive problems. Do everything numerically instead.
The first part of your code is straightforward but contains errors, corrected here:
a_0=(1/pi)*integral(@g1,-pi,pi, 'ArrayValued',1)
for n=1:10
a_n(n)=(1/pi)*integral(@(x)g1(x).*cos(n*x),-pi,pi, 'ArrayValued',1);
b_n(n)=(1/pi)*integral(@(x)g1(x).*sin(n*x),-pi,pi, 'ArrayValued',1);
end
That code runs.
I have no idea what you are doing in the last loop and the plot. In the numeric version, you need to define ‘x’. What do you want ‘f’ to be?
댓글 수: 2
Walter Roberson
2017년 1월 30일
편집: Walter Roberson
2017년 1월 30일
You used int() which is for the symbolic toolbox and does not work with regular functions. You need to use integral() with regular functions and you need to pass function handles with @
The code you have is requesting that g1 be called with no arguments and that symbolic integration should be applied to what it returns
If it is important to use the symbolic toolbox then you should change your g1 into a piecewise() expression.
If you switch to regular numeric functions then you should change your g1 to use logical indexing instead of if statements. integral() expects the function to be vectorized
참고 항목
카테고리
Help Center 및 File Exchange에서 Assumptions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!