how to create 1*101 of some integer array?

3 ビュー (過去 30 日間)
Meva
Meva 2015 年 4 月 5 日
コメント済み: Mohammad Abouali 2015 年 4 月 6 日
Hi,
Very basic que: I would like to have 1*101 of -4 array. I have to solve an integral numerically
int_0^x -4 dx;
My code is like:
x= linspace(0,1,0.01);
A = zeros(1,101);
..
cumtrapz(x,-4*A);
But the command prompt shows all zeros for cumtrapz which is certainly not.
If I just use :
cumtrapz(x,-4);
it says matrix dimensions must agree. How can I create this array?

採用された回答

Mohammad Abouali
Mohammad Abouali 2015 年 4 月 5 日
編集済み: Mohammad Abouali 2015 年 4 月 5 日
Yo have:
A = zeros(1,101);
and then later you have
cumtrapz(x,-4*A);
all elements of A are zero so -4*A is going to be still zero.
change A=zeros(1,101) to A=ones(1,101)
Also as imageAnalyst said, the third argument for linspace is not the spacing so you need to change the code to something like this
x=linspace(0,1,101);
A=ones(1,101);
cumtrapz(x,-4*A);
By the way, are you sure you want to use cumtrapz()? don't you want the output of trapz()?
  2 件のコメント
Meva
Meva 2015 年 4 月 6 日
編集済み: Meva 2015 年 4 月 6 日
As far as I know, trapz is for say
int_0^1
cumtrapz is working for
int_0^x
And the last element of cumtrapz is giving trapz.
So I guess cumtrapz is still the right choice. Please correct me if I am wrong.
Mohammad Abouali
Mohammad Abouali 2015 年 4 月 6 日
Both trapz(x,y) and cumtrapz(x,y) integrate between x and y for the entire domain.
Trapz gives you one number. But cumtrapz gives all the values from start to end.
So if x=xStart:dx:xEnd then trapz gives int_xStart^xEnd but cumtrapz will give an array the first one is for int_xStart^{xStart+dx) the second one is for int_xStart^{xStart+2dx} and the n-th number is for int_xStart^{xStart+n*dx}.
the last number of cumtrapz and trapz should be the same.
Both can be useful. just make sure which one is it that you want.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2015 年 4 月 5 日
The third argument of linspace is the number of elements, not the increment between elements. Try this:
numberOfElements = 101; % Whatever you want
x= linspace(0, 1, numberOfElements);
A = zeros(1, numberOfElements);

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by