ode 45 giving the wrong solution
이전 댓글 표시
hello i am trying to use ode command to solve this eqution using ode command :
function [ x_diff_ode ] = x_ode_method( t,x )
global A_a A_b B_a B_b f U t_on C_a C_b
ratio=rem(t,(1/f));
S_switch_a=ratio<t_on;
A_total=A_a*(S_switch_a)+A_b* not(S_switch_a);
B_total=B_a*(S_switch_a)+B_b*not(S_switch_a);
x_diff_ode= A_total*x+ B_total*U;
end
how ever this is giving me the wrong plot , and when i did it numericly using oilers method i got the the right plot ,here is the code using oile'rs method :
function [x_zigzag,t,z,V_R_oilers_theory,switch_a,t_switch] = eulers_method_numeric_sulution( A_a,A_b,B_a,B_b,f,U,t_on,C_a,C_b,number_of_points, number_of_cycles )
T=1/f;
% number_of_cycles=900;
% samples_per_cycle=300;
total_samples=number_of_points;
run_time=(T)*(number_of_cycles);
x1_zigzag=[0;0;0;0];
t=linspace(0,run_time, total_samples+1);
% t_delta=(T)/(samples_per_cycle);
t_delta=(run_time-0)/(total_samples+1);
for i=1:total_samples
ratio(i)=rem(t(i),(T));
S_switch_a(i)=ratio(i)>t_on;
A_total=A_a* S_switch_a(i)+A_b* not(S_switch_a(i));
B_total=B_a* S_switch_a(i)+B_b*not(S_switch_a(i));
C_total=C_a* S_switch_a(i)+C_b*not(S_switch_a(i));
x1_zigzag(:,i+1)=x1_zigzag(:,i)+t_delta*( A_total*x1_zigzag(:,i)+B_total*U);
V_R_oilers_theory(:,i+1)=C_total*x1_zigzag(:,i+1);
end
x_zigzag= x1_zigzag;
t=t;
z=t_delta;
V_R_oilers_theory=V_R_oilers_theory;
switch_a=S_switch_a;
t_switch=total_samples;
end
my question is why the ode command fails and giving me the wrong plot ? and how can i fix it ?
댓글 수: 3
Walter Roberson
2018년 2월 15일
Any time I see a global variable in an ode function I assume that it is empty or has an unexpected value. I do not bother to try debugging anything else about the ode function until the global variables have been eliminated.
Jan
2018년 2월 15일
"oilers method"?! The famous mathematician was called Euler, Leonhard Euler.
James Tursa
2018년 2월 15일
Maybe Wayne Gretzky is involved with this somehow ...
답변 (1개)
Jan
2018년 2월 15일
why the ode command fails and giving me the wrong plot
You did not show us your "ode command".
Your function to be integrated contains a discontinuity:
S_switch_a = ratio < t_on;
This is a DON'T for the numerical integration, because standard integrators as Matlab ODE45 are designed to handle smooth functions only. See http://www.mathworks.com/matlabcentral/answers/59582#answer_72047
카테고리
도움말 센터 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!