MATLAB: Undefined function ‘int’ for input arguments of type ‘char’.

charintunderfined function

Hi Guys
this code gives me this error when I tried to change it a little
Any solutions?
syms n t ;
n=50;
t=linspace(-5,5,1000);
T=max(t);
wo=(2*pi)/T;
for k=1:n
ao= (1/T).*(int('1',t,0,t));
an= (2/T).*(int('1*cos(n*wo*t)',t,0,t))
bn= (2/T).*(int('1*sin(n*wo*t)',t,0,t))
f=ao+(an.*cos(k.*wo.*t))+(bn.*sin(k.*wo.*t));
end
figure(1);clf(1)
plot(t,f)
xlabel('Time')
ylabel('Amplitude')
title('Fourier sawtooth Trasnform')

Best Answer

You have
syms n t ;
which is equivalent to
n = sym('n');
t = sym('t');
which make connections between those variables in the MATLAB workspace and symbolic variables with those names inside the symbolic engine.
You have
n=50;
t=linspace(-5,5,1000);
This breaks the connection between the variables in the MATLAB workspace and the variables in the symbolic engine, because you wrote over variables with the same names.
You have
ao= (1/T).*(int('1',t,0,t));
You replaced t with a numeric variable, so this is
int('1', NUMERIC_VECTOR, 0, NUMERIC_VECTOR)
None of the parameters are object-oriented objects, so the first parameter is used as the data type to search for a matching method. There is no method int for character vectors, so you get a failure.
If you had used
ao= (1/T).*(int(sym('1'),t,0,t));
then since there was an object-oriented parameter, the int method for that object would be looked up and found, so symbolic int would be called. However, this case would be
int(SYMBOL, NUMERIC_VECTOR, 0, NUMERIC_VECTOR)
and symbolic int cannot have a numeric vector as it second argument.
So what you should have done is not re-used the variable 't' between the syms t and the t = linspace(...) . Use different variable names for the two, and use the appropriate variable names in the appropriate positions in the int() call.
If syms t was still in effect, then the call would be
int(CHARACTER_VECTOR, SYMBOL, 0, SYMBOL)
which would invoke symbolic int, which would automatically transform it to equivalent to
int(sym(CHARACTER_VECTOR), SYMBOL, 0, SYMBOL)
and do the integration. Perhaps
syms n t ;
n=50;
trange=linspace(-5,5,1000);
T=max(trange);
wo=(2*pi)/T;
for k=1:n
thist = trange(k);
ao= (1/T).*(int('1',t,0,thist));
an= (2/T).*(int('1*cos(n*wo*t)',t,0,thist))
bn= (2/T).*(int('1*sin(n*wo*t)',t,0,thist))
f(k) = ao+(an.*cos(k.*wo.*thist))+(bn.*sin(k.*wo.*thist));
end
figure(1);clf(1)
plot(t,f)
xlabel('Time')
ylabel('Amplitude')
title('Fourier sawtooth Trasnform')