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;

回答 (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

1 件のコメント

This is a more reader friendly version of the script I wrote. Thx

サインインしてコメントする。

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

カテゴリ

質問済み:

2017 年 8 月 7 日

回答済み:

2017 年 8 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by