How to write a code for varying step size ?

42 views (last 30 days)
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 Comment
James Tursa
James Tursa on 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?

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 27 Sep 2017
Edited: Andrei Bobrov on 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 Comments
Andrei Bobrov
Andrei Bobrov on 29 Sep 2017
Hi Revathy!
If this answer solve problem from your question, please, "accept" him.

Sign in to comment.

More Answers (3)

James Tursa
James Tursa on 26 Sep 2017
Edited: James Tursa on 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 Comments
revathy M
revathy M on 27 Sep 2017
Hi! What's the difference between the above two syntax?

Sign in to comment.


Suganthi D
Suganthi D on 7 Oct 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 on 1 Mar 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 Comment
Janet onoja
Janet onoja on 4 Apr 2023
I having been trying to get code on variable step size using adams method but I can't. Any help please.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!