# Need help inputting my equation into MATLAB

1 view (last 30 days)
Dex Feliciano on 15 Oct 2020
Commented: Dex Feliciano on 15 Oct 2020
(R*(sin((pi*t)/12))^4 + R0)*(Q) - (Q+P)*((M(t))/((V-Q)*t))
The above equation is what I want to input, like this....
%% seawater.m
% Setup equation parameters
V = 10000; Q = 1000; P = 200; R = 10; R0 = 25; M0 = 0;
% Choose a time step h for Euler's method and the inteval [0,tf]
h = 0.01; tf = 120;
t = 0:h:tf;
M = zeros(1,length(t));
M(1) = M0;
% Enter the right side of the differential equation for M
% in terms of P, Q, R, R0 and V.
f = @(t,M) (R*(sin((pi*t)/12))^4 + R0)*(Q) - (Q+P)*((M(t))/((V-Q)*t));
% Implement Euler's method
for n=1:length(t)-1
M(n+1) = M(n) + f(t(n),M(n))*h;
end
% Plot the solution
plot(t,M/V), ylim([0,35]), grid('on')
title('Dilution of sea water')
xlabel('Time (h)'), ylabel('Salt concentration (g/L)')
However I get the following error:
Array indices must be positive integers or logical values.
Error in seawater>@(t,M)(R*(sin((pi*t)/12))^4+R0)*(Q)-(Q+P)*((M(t))/((V-Q)*t)) (line 13)
f = @(t,M) (R*(sin((pi*t)/12))^4 + R0)*(Q) - (Q+P)*((M(t))/((V-Q)*t));
Error in seawater (line 17)
M(n+1) = M(n) + f(t(n),M(n))*h;

madhan ravi on 15 Oct 2020
In line f, M(t) causes the error perhaps you mean‘t M*t ?
##### 2 CommentsShowHide 1 older comment
Dex Feliciano on 15 Oct 2020 Heres the equation I had at the end of question 4a. My differential equation.
The problem is in my M(t). My M is not a function in MATLAB so it can't take t as input variables. My M is a vector.
I just dont know how to factor in the M(t) part when entering my equation?