using the trapz function

6 ビュー (過去 30 日間)
Eliraz Nahum
Eliraz Nahum 2018 年 10 月 11 日
コメント済み: Walter Roberson 2018 年 10 月 11 日
hey, I am trying to understand how the trapz works. I am trying to compare its results to an exact solution, but for some reason can't get the same results.
clear all
close all
clc
syms x1 y1
y1=x1^2;
y1_int=int(y1);
y1_int_f=matlabFunction(y1_int)
y_int_exact=[];
del=0.1;
x=[-5:del:5];
y=x.^2;
counter=0;
for Xval=x
counter=counter+1;
y_int_exact(counter)=y1_int_f(Xval)-y1_int_f(x(1));
y_temp=y(1:counter);
y_int_calc(counter)=trapz(y_temp);
end
plot(x,y,'b',x,y_int_calc,'--r',x,y_int_exact,'g')

採用された回答

Eliraz Nahum
Eliraz Nahum 2018 年 10 月 11 日
according to https://uk.mathworks.com/help/matlab/ref/trapz.html I can only give the trapz function the y values of y(x). That's what I tried to do...each time giving the function a y_temp vector that contains the [y(1),y(2),...,y(counter)] values so it will find the area in each step
  1 件のコメント
Walter Roberson
Walter Roberson 2018 年 10 月 11 日
You should have read slightly further
Q = trapz(X,Y) integrates Y with respect to the coordinates or scalar spacing specified by X.
If X is a vector of coordinates, then length(X) must be equal to the size of the first dimension of Y whose size does not equal 1.
If X is a scalar spacing, then trapz(X,Y) is equivalent to X*trapz(Y).

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 10 月 11 日
You are not telling it what spacing to the trapz() call. Also your y_int_exact should probably be taking into account the spacing between values.

カテゴリ

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

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by