Need help inputting my equation into MATLAB

(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;

답변 (1개)

madhan ravi
madhan ravi 2020년 10월 15일

0 개 추천

In line f, M(t) causes the error perhaps you mean‘t M*t ?

댓글 수: 2

Dex Feliciano
Dex Feliciano 2020년 10월 15일
Heres my question/
For part a I got the equation I stated earlier. It features a function M(t), which I dont know how to implement into MATLAB.
Dex Feliciano
Dex Feliciano 2020년 10월 15일
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?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

질문:

2020년 10월 15일

댓글:

2020년 10월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by