Numerical analysis please help

1 回表示 (過去 30 日間)
francis
francis 2013 年 7 月 26 日
if true
i=1;
for q=0:0.05:4;
x(i)=q;
s(i)=quad('sin(x.^2)',0,x(i));
c(i)=quad('cos(x.^2)',0,x(i));
i=i+1;
end
fprintf('\n The value of s(x)=%5.3f',s(i-1))
fprintf('\n The value of c(x)=%5.3f\n',c(i-1))
plot(x,s,x,c)
xlabel('x')
ylabel(' C(x) and S(x)')
grid
figure
plot(c,s)
xlabel('c'),ylabel('s')
end
  4 件のコメント
Mike Hosea
Mike Hosea 2013 年 7 月 31 日
I got no error. What version of MATLAB are you using?
Andrew Reibold
Andrew Reibold 2013 年 7 月 31 日
I am using 2012b I believe, and I also received no errors. I did get two sexy looking graphs though.

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

回答 (1 件)

dpb
dpb 2013 年 7 月 26 日
Replace the loop structure
i=1;
for q=0:0.05:4;
x(i)=q;
s(i)=quad('sin(x.^2)',0,x(i));
c(i)=quad('cos(x.^2)',0,x(i));
i=i+1;
end
with
q=[0:0.05:4]';
s=accumarray([1:length(q)]',q,[],@(x) quad('sin(x.^2)',0,x));
c=accumarray([1:length(q)]',q,[],@(x) quad('cos(x.^2)',0,x));
If you want to get even more clever, define the functions as function handles... :)
I didn't pursue the initial too much; you should at a minimum preallocate the vectors s and c

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by