How to write a code for varying step size ?

69 次查看(过去 30 天)
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 个评论
James Tursa
James Tursa 2017-9-26
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?

请先登录,再进行评论。

采纳的回答

Andrei Bobrov
Andrei Bobrov 2017-9-27
编辑:Andrei Bobrov 2017-9-27
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
>>

更多回答(3 个)

James Tursa
James Tursa 2017-9-26
编辑:James Tursa 2017-9-26
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 个评论
revathy M
revathy M 2017-9-27
Hi! What's the difference between the above two syntax?

请先登录,再进行评论。


Suganthi D
Suganthi D 2021-10-7
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 2022-3-1
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 个评论
Janet onoja
Janet onoja 2023-4-4
I having been trying to get code on variable step size using adams method but I can't. Any help please.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Nonlinear Optimization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by