Differentiation using diff command
1 次查看(过去 30 天)
显示 更早的评论
Hi I am using diff command and in the end I need to compare the actual differential value with the one I am getting after using diff command, here is my code
Please tell me how can I get the correct value so as the relation can be built between the error and step size, i.e smaller the step size( or in other words greater the samples) smaller will be the error and results will get closer to the actual value my code is:
dt=0.01; %dt=1,0.1,0.01
t=0:dt:2*pi;
x=sin(t);
yt=cos(t); %mathematical differential
dx=diff(x)./dt;
dx=[0;dx(:)]'% Making its length equal to the vector yt since we are padding a zero here it gives incorrect results
y_sum=sum(yt)
x_sum=sum(dx)
error=yt-dx;
sum_error=sum(error)
Because this code is giving me opposite results
2 个评论
Walter Roberson
2019-2-3
When you calculate
y_sum=sum(yt)
you are approximating integral of cos(t) from t = 0 to t = 2 * pi, if you were multiplying by dt. But your dx has been divided by dt.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surrogate Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!