ode 45 giving the wrong solution
Show older comments
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 Comments
Walter Roberson
on 15 Feb 2018
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
on 15 Feb 2018
"oilers method"?! The famous mathematician was called Euler, Leonhard Euler.
James Tursa
on 15 Feb 2018
Maybe Wayne Gretzky is involved with this somehow ...
Answers (1)
Jan
on 15 Feb 2018
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
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!