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.
0 件のコメント
回答 (3 件)
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 件のコメント
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
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)
0 件のコメント
f4r3in
2017 年 1 月 25 日
2 件のコメント
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
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 Center および File Exchange で Assumptions についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!