using the trapz function
Show older comments
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')
Accepted Answer
More Answers (1)
Walter Roberson
on 11 Oct 2018
0 votes
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.
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!