Need help inputting my equation into MATLAB

1 回表示 (過去 30 日間)
Dex Feliciano
Dex Feliciano 2020 年 10 月 15 日
コメント済み: Dex Feliciano 2020 年 10 月 15 日
(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 日
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?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePhysics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by