how with ode45 order we can solve a switching problem?
Show older comments
Hi every one.
I want to write a code like this with ode45. how can I do it?
ode45(func)
func:
mode 1;
if x>=550
mode =2;
if mode=2 and x<=510
mode=1;
x'=f(x)= 0.1*x-50 mode =1
0.1*x-56 mode=2
I write this code, but gives me not correct output in figure
clc;
clear all;
close all;
deltat=10^-2;
x1(1)=510;
x11(1)=550;
for i=1:1000
x1(i+1)=(0.1*x1(i)-50)*deltat+x1(i);
y1(i)=x1(i);
if x1(i+1)>=550 && x1(i+1)==(0.1*x1(i)-50)*deltat+x1(i)
%return i;
x1(i+1)=(0.1*x1(i)-56)*deltat+x1(i);
y1(i)=x1(i);
elseif x1(i+1)<=510 && x1(i+1)==(0.1*x1(i)-56)*deltat+x1(i)
%return i;
x1(i+1)=(0.1*x1(i)-50)*deltat+x1(i);
y1(i)=x1(i);
end
end
figure(1)
plot(i,y1(i))
And what may I do to ruturn "i" as index when "x" reaches 550 or 510?
writing the above code "return i" it gives this error:
Error: File: ntest.m Line: 12 Column: 16
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax
error. To construct matrices, use brackets instead of parentheses.
Thanks
Answers (2)
Stephan
on 29 Jul 2019
0 votes
4 Comments
azam ghamari
on 29 Jul 2019
azam ghamari
on 31 Jul 2019
Stephan
on 1 Aug 2019
At the moment im on vacation and have no access to my computer.
azam ghamari
on 30 Jul 2019
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!