parse error at clear : usage might be invalid MATLAB syntax

3 次查看(过去 30 天)
hi this is my matlab programming :
function y=f1(x)
if x>0
y=1;
elseif x<0
y=-1;
end
end
clear
clc
syms x
g=f1(x);
N=10;
a_0=(1/2*pi)*int(g,-pi,pi);
for n=1:N
a_n(n)=(1/pi)*int(g*cos(n*x),-pi,pi);
b_n(n)=(1/pi)*int(g*sin(n*x),-pi,pi);
end
a_n
b_n
g_new=a_0;
for n=1:N
g_new=g_new+a_n(n)*cos(n*x)+b_n(n)*sin(n*x);
end
subs(g_new,x,5)
subs(g,x,5)
ezplot(x,g_new)
and I give this error :
Error: File: f1.m Line: 8 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition
of the function "f1".)
and when mouse go over the line 8 I give the message :
parse error at clear : usage might be invalid MATLAB syntax
please help me.

回答(3 个)

Guillaume
Guillaume 2017-1-22
As of R2016b you can have local functions in scripts but these must be after the script, not before.
Move your function f1 to the end of the script. Additionally, you may want to fix the bug that the function does not defines y when x is exactly 0.
A simple definition of f1 would be:
function y = f1(x)
y = sign(x); %i.e. you don't actually need f1, you could just use sign in the script.
end
  3 个评论
f4r3in
f4r3in 2017-1-23
thanks for answering my problem solved by using "sign(x)" but if I want to use function what should I do ??? as you said I move function to the end of the script but my problem is standing yet.
clear
clc
syms x
g=f1(x);
N=10;
a_0=(1/2*pi)*int(g,-pi,pi);
for n=1:N
a_n(n)=(1/pi)*int(g*cos(n*x),-pi,pi);
b_n(n)=(1/pi)*int(g*sin(n*x),-pi,pi);
end
a_n
b_n
g_new=a_0;
for n=1:N
g_new=g_new+a_n(n)*cos(n*x)+b_n(n)*sin(n*x);
end
subs(g_new,x,5)
subs(g,x,5)
ezplot(x,g_new)
function y=f1(x)
if x>0
y=1;
elseif x<0
y=-1;
end
end
now please help me.
Guillaume
Guillaume 2017-1-23
编辑:Stephen23 2017-1-30
As far as I can tell your script is fine. I don't have the symbolic toolbox, so can't test.
However, as stated you need R2016b to have local functions in scripts. If you're in on an earlier version, the function will have to be in its own file.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2017-1-23
You are passing the symbolic x to f1 in the call f1(x) . In that call you compare the received x to a value. The only comparison you can make for symbols is "==" or "~=" and even those fail most of the time (and tell you to use isAlways())
You would need to switch your function to use piecewise() (R2016b or later), or to use heaviside . If you do use heaviside, watch out for the result of heaviside(0)

f4r3in
f4r3in 2017-1-25
thanks for all answers but your answer doesn't work and I solve my problem by myself. it solve very easy I must put "end" of function at the end of the script. like this :
but now I have another problem in running this script and I receive this error : --->>
Conversion to logical from sym is not possible. Error in g1 (line 2) if x>0
  2 个评论
Guillaume
Guillaume 2017-1-25
编辑:Guillaume 2017-1-25
Yes, you solved the problem, the same way that burning the house down would solve the problem of that little dirt mark on the door mat. You may have got rid of the particular error you were getting, it does not mean it's the correct solution. Even if you didn't get the conversion error, you'd have the problem that your function keep calling itself forever (well, until you get a recursion error), because moving the script code into the function is completely the wrong thing to do.
As have been said several times now:
  • If you're using R2016b only move the function after the script code.
  • Any other version of matlab, move the function in its own file.
Also pay attention to Walter's answer. I know nothing about the symbolic toolbox so can't comment but I'm certain he is correct.
Jan
Jan 2017-1-26
It does not look, like you have solved the problem. Now you code calls itself recursively. I don't think, that this is intented.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Assumptions 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by