Write a script that prompts the user to input a function, f(x),

1 回表示 (過去 30 日間)
libin danial
libin danial 2012 年 8 月 2 日
Write a script that prompts the user to input a function, f(x), and the limits of integration, a and b. The script should approximate the integral of the function from a to b, using the Trapezoid Rule. The script should also generate a plot of the function AND the piecewise-linear approximation to the function used in the Trapezoid Rule.
this is what i have soo far
clc,clear all, close all
f=str2func(['@(x)' input('Enter f(x):','s')]);
x=linspace(a,b,N);
N=input('define n=');
a=input('insert lower limit=');
b=input('insert upper limit=');
x=linspace(a,b,N);
h=(b-a)/(N-1);
for i=1:N-1;
T=T+h*(((f(x(i)))+(f(x(i+1))))/2);
end
plot(a,b)
am i doing something wrong
  1 件のコメント
Oleg Komarov
Oleg Komarov 2012 年 8 月 2 日
編集済み: Oleg Komarov 2012 年 8 月 2 日
You can aslo safely drop the too many ()
h*(f(x(i))+f(x(i+1)))/2

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

採用された回答

Babak
Babak 2012 年 8 月 2 日
In the first line of your code you cannot say
x=linspace(a,b,N);
because a, b and N are not yet defined there.
  1 件のコメント
Babak
Babak 2012 年 8 月 2 日
remove that line... you also need to use
for i=1:N-1
insetad of
for i=1:N-1;
I mean without semicolon.
In your statement:
T=T+h*(((f(x(i)))+(f(x(i+1))))/2);
T needs to be defined in Advance... you can sat T=0; at the beginning of your code for example.
Note: I recommend you using func() or fun() instead of f() because MATLAB has issues evaluating f(g(x)) when function names f, g are single characters.

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

その他の回答 (1 件)

libin danial
libin danial 2012 年 8 月 2 日
thanks, how about the piloting part does that look right

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by