Help me with this task using (Euler`s method)

11 ビュー (過去 30 日間)
Kamal Mohamed
Kamal Mohamed 2020 年 12 月 14 日
コメント済み: Kamal Mohamed 2020 年 12 月 14 日
Question:
Use this method to solve the following IVP:
A series RC circuit with R = 5 W and C = 0.02 F is connected with a battery of E = 100 V. At t = 0, the voltage across the capacitor is zero.
(a) Obtain the subsequent voltage across the capacitor.
(b)find the true error if the exact solution is: Vc = V(1e^(-t/RC))
Hint:
Use the formula
r(dq/dt)+(q/c)=v
---------------------------------------------
It is giving me msg operator '*' is not supported for operands of type 'function_handle'
also i wanna ypu give me advices on my work if i working right or wrong to improve as it`s my first time to use matlab
My Work:
R=5;
C=0.02;
E=100;
Y0=0;
Xinitial=0;
Xfinal=1;
h=0.01;
X=Xinitial:h:Xfinal;
Y = zeros(size(X));
N=(Xfinal-Xinitial)/h;
for i=1:N-1
Ydot=@(t,q) (E-q/C)/R;
Y(i+1) = Y(i) + h*(Ydot);
end
Vreuler= Y0 / C;
Exact=E * (1 - exp(-X/(R*C)));
Error=(Exact-Vreuler)/Exact;
polt(x.y); grid onh

採用された回答

James Tursa
James Tursa 2020 年 12 月 14 日
編集済み: James Tursa 2020 年 12 月 14 日
The function handle is something you only need to create once, before the for-loop starts. Then call that function handle with your state inside the loop. E.g.,
Ydot = @(t,q) (E-q/C)/R;
Y(1) = Y0;
for i=1:N-1
Y(i+1) = Y(i) + h*Ydot(X(i),Y(i));
end
Then the plot would be
plot(X,Y,X,Exact); grid on
And this is a more robust way of defining N:
N = numel(X);
  3 件のコメント
James Tursa
James Tursa 2020 年 12 月 14 日
編集済み: James Tursa 2020 年 12 月 14 日
MATLAB is case sensitive. You had used uppercase X in your first post and you need to stay with that. E.g.,
Y = zeros(size(X)); % UPPERCASE X, not lowercase x
Also your plot needs to change that period to a comma and spell plot properly
plot(X,Y,X,Exact); grid on
Kamal Mohamed
Kamal Mohamed 2020 年 12 月 14 日
Okay Thanks so much :D

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by