forward approximation of derivative

3 ビュー (過去 30 日間)
ray sanchez
ray sanchez 2015 年 5 月 13 日
コメント済み: Titus Edelhofer 2015 年 5 月 15 日
I'm trying to figure out how to plot the forward approximation of the derivative of 1000^(x/17) at x=3.2 for h values of 0.01 to 5 in steps of 0.05. Here is what i have so far.
clear; close all; clc;
delx = 0.05;
x = 0.01:delx:5;
y = 1000.^(x/17);
fig = figure();
set(fig,'color','white');
plot(x,y,'linewidth',2);
xlabel('x');
ylabel('y');
grid on;
yforward = (y(2:end-1) - y(1:end-1))./delx;
hold on
plot(x(1:end)-delx/2,yforward,'r-','linewidth',2);
I'm getting an error that says matrix dimensions must agree. I also don't know how to plot the derivative at x = 3. Any help appreciated.

採用された回答

Titus Edelhofer
Titus Edelhofer 2015 年 5 月 13 日
Hi,
first of all, your yforward should be
yforward = (y(2:end) - y(1:end-1))./delx;
which is in fact the same as
yforward = diff(y) ./ delx;
When it comes below to plotting: your last point has no forward derivative anymore, therefore yforward is shorter than x by one element. Try to plot
plot(x(1:end-1)-delx/2,yforward,'r-','linewidth',2);
Titus
  2 件のコメント
ray sanchez
ray sanchez 2015 年 5 月 13 日
Thanks that was helpful! I'm still trying to figure out how to output the value for both at x=3.2 and the error.
Titus Edelhofer
Titus Edelhofer 2015 年 5 月 15 日
Hi,
look for the entry in x closest to 3.2:
[~,idx] = min(abs(x-delx/2-3.2));
Get the derivative at this point:
der = yforward(idx);
and plot the line:
h=plot(x(1:end-1)-delx/2, y(idx) + (x(1:end-1)-delx/2-3.2) * der);
That should also help with your question, otherwise please reformulate ...
Titus

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by