Why this program keep saying Error using plotting>getC Too many output arguments. Error in plotting (line 9) res(i)=getC(1,0,100);? The getC returns c, which is only one output(a number),where're the 'too many' outputs?

Here's my scripts,I'm new to Matlab and really couldn't figure out how to fix this problem...
function plotting
clc
close all
x = linspace(1,2,2);
res=zeros(1,length(x));
for i=1:length(x)
xpt=x(i);
res(i)=getC(1,0,100);
%res(i)=getC;
end
plot(x,res)
function getC(x,t,n)
c=0;
for k=1:1:n
c=c+(4*(-1).^k)/((2*k+1)*pi)*cos(((2*k+1)*pi)*x/2)*exp(((-(2*k+1)*pi)/2).^2*t);
end
getC = c;

Answers (2)

if true
function plotting
clc close all
x = linspace(1,2,2);
res=zeros(1,length(x));
for i=1:length(x)
xpt=x(i);
res(i)=getC(1,0,100);
%res(i)=getC; end plot(x,res)
if true
function getC(x,t,n)
c=0;
for k=1:1:n
c=c+(4*(-1).^k)/((2*k+1)*pi)*cos(((2*k+1)*pi)*x/2)*exp(((-(2*k+1)*pi)/2).^2*t);
end
getC = c;
end
Change
function getC(x,t,n)
c=0;
for k=1:1:n
c=c+(4*(-1).^k)/((2*k+1)*pi)*cos(((2*k+1)*pi)*x/2)*exp(((-(2*k+1)*pi)/2).^2*t);
end
getC = c;
to
function c = getC(x,t,n)
c=0;
for k=1:1:n
c=c+(4*(-1).^k)/((2*k+1)*pi)*cos(((2*k+1)*pi)*x/2)*exp(((-(2*k+1)*pi)/2).^2*t);
end

Asked:

on 7 Aug 2017

Answered:

on 7 Aug 2017

Community Treasure Hunt

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

Start Hunting!