parse error at .... :usage might be invalid matlab syntax

240 visualizzazioni (ultimi 30 giorni)
f4r3in
f4r3in il 29 Gen 2017
Commentato: Guillaume il 30 Gen 2017
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".)

Risposte (2)

Stephen23
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
Stephen23
Stephen23 il 29 Gen 2017
Modificato: Stephen23 il 29 Gen 2017
I know you will get the same error: your MATLAB version does not support both functions and scripts in one Mfile.
As my answer said, move your function to its own Mfile, named "g1.m".
Guillaume
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.

Accedi per commentare.


Star Strider
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
f4r3in
f4r3in il 30 Gen 2017
I run your code but the output (answers) was wrong. my code works on one input functions like g = x but it doesn't work on 2 input functions like g1. for one input functions answer must be like this :
syms x y
g=x;
a_0=(1/pi)*int(g,-pi,pi)
for n=1:10
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
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)
and output like this :
a_0 =
0
a_n =
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
b_n =
[ (5734161139222659*pi)/9007199254740992, -(5734161139222659*pi)/18014398509481984, (1911387046407553*pi)/9007199254740992, -(5734161139222659*pi)/36028797018963968, (5734161139222659*pi)/45035996273704960, -(1911387046407553*pi)/18014398509481984, (5734161139222659*pi)/63050394783186944, -(5734161139222659*pi)/72057594037927936, (1911387046407553*pi)/27021597764222976, -(5734161139222659*pi)/90071992547409920]
but in 2 input functions I receive this error:
Not enough input arguments.
Error in g1 (line 2)
if x>0
Error in Untitled (line 3)
a_0=(1/pi)*int(g1,-pi,pi)
Walter Roberson
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

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by