how with ode45 order we can solve a switching problem?

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

Antworten (2)

Stephan
Stephan am 29 Jul. 2019

0 Stimmen

4 Kommentare

I go ahead and chenge my program to this:
function dx = rigid(t,x)
dx=zeros(1,1);
dx=0.1*x-50;
if dx==0.1*x-50 && x>=550
dx=0.1*x-56;
elseif dx==0.1*x-56 && x<=510
dx=0.1*x-50;
end
if dx==0.1*x-50 && x>=550
dx=0.1*x-56;
end
for rigid function and for calling it in another mfile I write this code:
Close all;
clc;
[T,X] = ode45(@rigid,[0,100],[510]);
% figure(1)
% dxxxx = rigid([0,100],[510])
plot(T,X)
but it does not give me the right answer. I expect that it gives me a curve between 510 , 550 . but it gives me a curve starting from 510 and goaes up to 1700, it does not switch between these 2 functions (dx=0.1x-50 , dx-0.1x-56)why?
Stephan
Stephan am 29 Jul. 2019
Bearbeitet: Stephan am 29 Jul. 2019
Did you read the informations from the suggested link carefully? The event function is an extra function, so that you dont need a control structure to achieve a switching behaviour during the integration
Hi, Two days ago I answered you, that I actually read it and I don't know how shall I use the odeXY. please see my answer in below.
and may you please write that code (that I wrote with ode45) with ODEXY?
Thanks and Best Reagards.
At the moment im on vacation and have no access to my computer.

Melden Sie sich an, um zu kommentieren.

azam ghamari
azam ghamari am 30 Jul. 2019
I see it , but I am not sure to undrestand some parts for example:
isterminal(i) = 1 if the integration is to terminate when the ith event occurs. Otherwise, it is 0.
what does it mean exactly?
and I write the code without ode:
clc;
clear all;
close all;
deltat=1;
x1(1)=510;
x11(1)=550;
x1(2)=(0.1*x1(1)-50)*deltat+x1(1);
for i=1:1000
y1(i)=x1(i);
if (x1(i+1)>=550 && x1(i+1)==(0.1*x1(i)-50)*deltat+x1(i))||(x1(i+1)<=550 && x1(i+1)==(0.1*x1(i)-56)*deltat+x1(i))
%return i;
x1(i+2)=(0.1*x1(i+1)-56)*deltat+x1(i+1);
y1(i)=x1(i);
elseif (x1(i+1)<=510 && x1(i+1)==(0.1*x1(i)-56)*deltat+x1(i))||(x1(i+1)>=510 && x1(i+1)==(0.1*x1(i)-50)*deltat+x1(i))
%return i;
x1(i+2)=(0.1*x1(i+1)-50)*deltat+x1(i+1);
y1(i)=x1(i);
end
end
figure(1)
plot(x1)
Iand it gives me the result, and I want to write teh code with ode function , how can I change my before code taht I sent befor (with ode) based on this code in away that I get the result ?(with odeXY that you introduced)
Thanks

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2019a

Tags

Gefragt:

am 29 Jul. 2019

Kommentiert:

am 1 Aug. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by