Filter löschen
Filter löschen

How to write a code for varying step size ?

14 Ansichten (letzte 30 Tage)
revathy M
revathy M am 26 Sep. 2017
Kommentiert: Janet onoja am 4 Apr. 2023
Hi! I'm working on boundary layer theory.I've written a code for solving block tri-diagonal matrix.One of the variable i've taken with variable step size to get a good accuracy.Have used if loop .While executing it takes the first if condition and executes for the whole interval as my initialization is 0.As my incremental value is the varying parameter how to bring it?
x_bar=0;
if x_bar<=0.5
delx_bar=0.01;
elseif x_bar >=0.5 && x_bar <=1.25
delx_bar=0.005;
else
delx_bar=0.0001;
end
%%Intial profiles start
x_bar=0:delx_bar:1.25;
n_xbar=length(x_bar);
As I've used x_bar and length of x_bar in the code for calculation of values i need to define it. What changes do I have to make so that my x_bar values are generated first and then the successive delx_bar are taken for calculation of values.
  1 Kommentar
James Tursa
James Tursa am 26 Sep. 2017
What do you want for a result? A vector x_bar that starts at 0, then grows by 0.01 until it reaches the value 0.5, then grows by the value 0.005 until it reaches the value 1.25, then grows by 0.0001 until it reaches some final value?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 27 Sep. 2017
Bearbeitet: Andrei Bobrov am 27 Sep. 2017
Xb = [0, .5, 1.25, 2];
dx = [.01, .005, .0001];
x_bar = cumsum([Xb(1), repelem(dx,diff(Xb)./dx)]);
>> x_bar_j = 1.589
delx_bar_j = dx(discretize(x_bar_j,Xb))
x_bar_j =
1.589
delx_bar_j =
0.0001
>>
  2 Kommentare
revathy M
revathy M am 27 Sep. 2017
Thank you.
Andrei Bobrov
Andrei Bobrov am 29 Sep. 2017
Hi Revathy!
If this answer solve problem from your question, please, "accept" him.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

James Tursa
James Tursa am 26 Sep. 2017
Bearbeitet: James Tursa am 26 Sep. 2017
Does this do what you want? (Using arbitrary final value of 2.000 for example)
xstart = 0.000; xend = 0.500; dx = 0.01;
part1 = linspace(xstart ,xend,round((xend-xstart)/dx)+1);
xstart = 0.500; xend = 1.250; dx = 0.005;
part2 = linspace(xstart+dx,xend,round((xend-xstart)/dx) );
xstart = 1.250; xend = 2.000; dx = 0.0001;
part3 = linspace(xstart+dx,xend,round((xend-xstart)/dx) );
x_bar = [part1,part2,part3];
  4 Kommentare
revathy M
revathy M am 27 Sep. 2017
Hi! Thank you. Will try .
revathy M
revathy M am 27 Sep. 2017
Hi! What's the difference between the above two syntax?

Melden Sie sich an, um zu kommentieren.


Suganthi D
Suganthi D am 7 Okt. 2021
Hi professor , a very happy morning. I have written a code for step input variation for 10 houses ..Time for 24 hours. But i do not is this correct or not.. I need your help .. Here I have attached the code .. I want the graph for 10 houses step variation at 24 hours.. how to write the code for tis one..
T=1:1:24; %hours
input = createStep('StepTime',5,'StepSize',5,'FinalTime',25)
plot(input);

AKASH KUMAR
AKASH KUMAR am 1 Mär. 2022
clc
clear
close all
%% Variable step size
Step_0=2;
Step=Step_0;
ii=1;theta=0;
while true
theta= theta+Step;
th(ii)=(theta);
Y(ii)=sind(th(ii));
if Y(ii)>0.7 || Y(ii) <-0.7
Step=1/10*Step_0;
else
Step=Step_0;
end
ii=ii+1;
if theta>180*2
break
end
end
plot(th,Y,'.')
grid on;
xlabel('theta(deg)');
  1 Kommentar
Janet onoja
Janet onoja am 4 Apr. 2023
I having been trying to get code on variable step size using adams method but I can't. Any help please.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Nonlinear Optimization finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by