Trevor
Trevor

Reputation: 1

Matlab Differentiation

I need to write a for loop in matlab to solve a derivative using the forward difference method. The function to derive is 10+15x+20x^2 from 0 to 10 using steps of 0.25. I have tried using

h=.25; 
x=[0:h:10]; 
y = 10+15*x+20*x.^2; 
y(1) = 45; size(x)  
for i=2:47,     
       y(i) = y(i-1) + h*(15+40*x); 
end

Upvotes: 0

Views: 168

Answers (1)

fpe
fpe

Reputation: 2750

I'd do like this, as a start,

h=.25; 
x=[0:h:10]; 
y = 10+15*x+20*x.^2; 
diff(y)./diff(x)

or, as alternative,

syms x;
y = 20.*x.^2 + 15.*x + 10;
dy = diff(y,1);
h=.25; 
xx=[0:h:10];
res = subs(dy,xx);

Upvotes: 1

Related Questions