# help w euler code error

11 views (last 30 days)
Paige Macdonald on 16 Sep 2020
Edited: Rena Berman on 8 Oct 2020 at 17:15
%Euler's Method
p = 1;
t = 0:p:100;
S = zeros(size(t));
S(1) = 1.000; %initial concentration
n = numel(S); %number of y values (S is y in this case)
disp(' Time Reactant Concentration ')
disp(' (sec) (mol/L) ')
disp(' -------------------------------- ')
%loop to solve the eq
for i=1:n-1
f = -0.5 /(2.1+S)
S(i+1) = S(i) + p * f; %eulers formula
fprintf('6.0f%15.3f\n',t(i), S(i))
end
i am receiving an error for the f = -0.5 /(2.1+S) it says "Error using / Matrix dimensions must agree."

Rik on 16 Sep 2020
That is not an answer to the question Madhan posted. You got free help from a stranger on the internet. Why did you decide to remove your question? You could have simply not posted it. Now you are an inconsiderate jerk taking advantage of friendly people. Is that your intent?
Rik on 16 Sep 2020
%Euler's Method
p = 1;
t = 0:p:100;
S = zeros(size(t));
S(1) = 1.000; %initial concentration
n = numel(S); %number of y values (S is y in this case)
disp(' Time Reactant Concentration ')
disp(' (sec) (mol/L) ')
disp(' -------------------------------- ')
%loop to solve the eq
for i=1:n-1
f = -0.5 /(2.1+S)
S(i+1) = S(i) + p * f; %eulers formula
fprintf('6.0f%15.3f\n',t(i), S(i))
end
i am receiving an error for the f = -0.5 /(2.1+S) it says "Error using / Matrix dimensions must agree."
Rena Berman on 8 Oct 2020 at 17:15

James Tursa on 16 Sep 2020
You need to index S:
f = -0.5 /(2.1+S(i));