Adding an amount to a specified array in an expression.

I wrote code on the forward, backward, and central finite difference methods of a polynomial as well as the first and second true derivatives and plotted them. I was wondering if there is a simple way to name the polynomial and add the step size to x without having to write out each expression separately which I have already done. something like
f=expression
f1(x+h)= expressions with h added to each variable
here is the code
syms xx
f=x.^3-2.*x+4;
der1true=diff(f)
der2true=diff(f,2)
x=-2:.1:2;
h=.25;
d1f=(((x+h).^3-2.*(x+h)+4)-(x.^3-2.*x+4))/h
d1b=((x.^3-2.*x+4)-((x-h).^3-2.*(x-h)+4))/h
d1c=(((x+.5*h).^3-2.*(x+.5*h)+4)-((x-.5*h).^3-2.*(x-.5*h)+4))/(2*h)
d2f=(((x+2*h).^3-2.*(x+2*h)+4-(2*((x+h).^3-2.*(x+h)+4))+(x.^3-2.*x+4)))/h^2
d2b=((x.^3-2.*x+4)-(2*(x-h).^3-(2.*(x-h))+4)+((x-2*h).^3-2.*(x-2*h)+4))
d2c=(((x+h).^3-2.*(x+h)+4)-(2*(x.^3-2.*x+4))-((x-h).^3-2.*(x-h)+4))
subplot(1,2,1)
hold on
plot(der1true,'-m')
plot(d1f,'-.g')
plot(d1b,':k')
plot(d1c,'--r')
hold off
subplot(1,2,2);hold on
plot(der2true,'-m')
plot(d2b,':k')
plot(d2c,'--r')
plot(d2f,'-.g')
hold off
I think maybe the sub command but not sure
Thanks for any help provided

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by