parse error at clear : usage might be invalid MATLAB syntax
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (3)
Guillaume
il 22 Gen 2017
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 Commenti
Guillaume
il 23 Gen 2017
Modificato: Stephen23
il 30 Gen 2017
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
il 23 Gen 2017
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 Commenti
f4r3in
il 25 Gen 2017
2 Commenti
Guillaume
il 25 Gen 2017
Modificato: Guillaume
il 25 Gen 2017
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
il 26 Gen 2017
It does not look, like you have solved the problem. Now you code calls itself recursively. I don't think, that this is intented.
Vedere anche
Categorie
Scopri di più su Assumptions in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!