parse error at .... :usage might be invalid matlab syntax
187 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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".)
1 Commento
Risposte (2)
Stephen23
il 29 Gen 2017
Modificato: Stephen23
il 29 Gen 2017
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 Commenti
Guillaume
il 30 Gen 2017
@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
il 29 Gen 2017
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 Commenti
Walter Roberson
il 30 Gen 2017
Modificato: Walter Roberson
il 30 Gen 2017
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
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!