HELP: looping a function and plot (ode23)

3 ビュー (過去 30 日間)
Hade.R
Hade.R 2020 年 6 月 3 日
編集済み: Hade.R 2020 年 6 月 3 日
Hi, so the question for my exercise is : Write a MATLAB code to solve the differential equation for 𝑣𝑜𝑢𝑡 using function ode23() and plot the output. hint: to get the output waveform, plot(t, y(:,1))
The graph output was in subplot i believe. and the equation of each graph are the same except for the value of R. I tried doing the code with my little matlab knowledge and after searching the mathwork, i've tried to mix and match some coding that i think were relevent and make sense for my question. Here is my code;
script:
function ode23()
function myout=fun(t,y,R)
Vi=5; C=0.1; L=0.1;
if nargin < 0.5 || isempty(R) %i dont really know about this one
R = 0.5; % Your default value
end
myout=[(Vi-R*C*y(2)-y(1))/(L*C)];
end
end
command windows:
tspan=[0, 10]; ohm='x03A9';
y0=[0];
[t_r1, y_r1]=ode23(@(t,y) fun(t,y,0.5), tspan, y0);
plot(t_r1, y_r1(:,1))
[t_r2, y_r2]=ode23(@(t,y) fun(t,y,2), tspan, y0);
plot(t_r2, y_r2(:,1))
[t_r3, y_r3]=ode23(@(t,y) fun(t,y,4), tspan, y0);
plot(t_r2, y_r2(:,1))
subplot(3,1,1)
plot(t_r1, y_r1(:,1))
ylabel('Vout'), xlabel('Time(s)')
title('Underdamped response of R=0.5''ohm')
subplot(3,1,2)
plot(t_r2, y_r2(:,1))
ylabel('Vout'), xlabel('Time(s)')
title('Critically damped response of R=2''ohm')
subplot(3,1,3)
plot(t_r3, y_r3(:,1))
ylabel('Vout'), xlabel('Time(s)')
title('Over damped response of R=0.5''ohm')
If anyone can help me, that would be great. Thanks in advance.
  2 件のコメント
Steven Lord
Steven Lord 2020 年 6 月 3 日
function ode23()
Please don't name your function ode23. That name already has a meaning in MATLAB.
Hade.R
Hade.R 2020 年 6 月 3 日
yes, thanks alot! i just realized that ode23 is already a function so i misunderstood when the question say 'using function ode23()'. i thought the question want me to name the function as ode23..

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

採用された回答

Stephan
Stephan 2020 年 6 月 3 日
編集済み: Stephan 2020 年 6 月 3 日
tspan1 = [0, 4];
tspan2 = [4, 10];
y0 = [0, 0];
[t1_r1, y1_r1] = ode23(@(t,y) fun(t,y,0.5,5), tspan1, y0);
[t2_r1, y2_r1] = ode23(@(t,y) fun(t,y,0.5,0), tspan2, y1_r1(end,:));
[t1_r2, y1_r2] = ode23(@(t,y) fun(t,y,2,5), tspan1, y0);
[t2_r2, y2_r2] = ode23(@(t,y) fun(t,y,2,0), tspan2, y1_r2(end,:));
[t1_r3, y1_r3] = ode23(@(t,y) fun(t,y,4,5), tspan1, y0);
[t2_r3, y2_r3] = ode23(@(t,y) fun(t,y,4,0), tspan2, y1_r3(end,:));
subplot(3,1,1)
plot([t1_r1; t2_r1], [y1_r1(:,1); y2_r1(:,1)])
ylabel('Vout'), xlabel('Time(s)')
title('Underdamped response of R=0.5''ohm')
ylim([-5 10]);
subplot(3,1,2)
plot([t1_r2; t2_r2], [y1_r2(:,1); y2_r2(:,1)])
ylabel('Vout'), xlabel('Time(s)')
title('Critically damped response of R=2''ohm')
ylim([0 6])
%
subplot(3,1,3)
plot([t1_r3; t2_r3], [y1_r3(:,1); y2_r3(:,1)])
ylabel('Vout'), xlabel('Time(s)')
title('Over damped response of R=0.5''ohm')
ylim([0 6]);
function myout=fun(~,y,R,Vi)
C=0.1;
L=0.1;
myout = zeros(2,1);
myout(1)= y(2);
myout(2)=(Vi-R*C*y(2)-y(1))/(L*C);
end
  5 件のコメント
Stephan
Stephan 2020 年 6 月 3 日
編集済み: Stephan 2020 年 6 月 3 日
I will try:
  • As stated by Stephen Lord dont name your own functions like inbuilt functions in Matlab are named.
  • As Madhan stated the function is not continuos due to the stepped Vin signal. That means we have to perform 2 integration steps, the second one starting at the falling step signal by t=4.
  • That also means we need 2 tspans and the initial values for the second step are the last values of the first step.
  • Since your ode is second order, the function has to be rewritten as a system of 2 first order ode's. This is what happens.
  • Because your provided code (and the bugs in it...) did only provide one initial value, i first thought we talk about one first order ode and you ran wrong by using y(1) and y(2). When you provided the whole information, i understood which changes will be needed to get it run.
  • Last but not least i was on my way home in the meantime, so i could not fix it quickly.
BTW: If this helped you, dont miss to accept useful answers.
Hade.R
Hade.R 2020 年 6 月 3 日
編集済み: Hade.R 2020 年 6 月 3 日
thanks a lot Stephen! you really help me. i understand much better with your explaination. Thanks also for your time and i already accept the answer :)

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by