フィルターのクリア

I'm trying to integrate using Simpson's rule

3 ビュー (過去 30 日間)
ray sanchez
ray sanchez 2015 年 5 月 13 日
コメント済み: Walter Roberson 2015 年 5 月 14 日
I'm trying to integrate sin(x) from 0 to pi using the simpson's rule. The only thing is I need it to output several iterations each with a different number of intervals. For example, n= 4, 8 , 16, 32, 64. Here is my code so far clear; close all; clc;
ezplot('sin(x)', [0, pi]), hold on
f=@(x) sin(x);
a=0;b=pi;
n=4;
h=(b-a)/n;
p=0;
for i=a:b
p=p+1;
x(p)=i;
y(p)= sin(i);
plot(x, sin(x), 'r')
end
l=length(x);
x
y
any help appreciated!

回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 5 月 13 日
So instead of "n=4" use
for n = [4, 8, 16, 32, 64]
and put an "end" after.
  2 件のコメント
ray sanchez
ray sanchez 2015 年 5 月 13 日
Where would I put the "end"? I tried it in several places and it didn't work
Walter Roberson
Walter Roberson 2015 年 5 月 14 日
for n = [4, 8, 16, 32, 64]
h=(b-a)/n;
p=0;
for i=a:b
p=p+1;
x(p)=i;
y(p)= sin(i);
plot(x, sin(x), 'r')
drawnow();
end
l=length(x);
x
y
end
Right at the end.
Note that I also added a drawnow() call so you have a hope of seeing the plots you are generating. I have no idea why you would want to plot in the manner you are plotting, but I guess you must have a reason.

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

カテゴリ

Help Center および File ExchangeNumerical Integration and Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by