Effacer les filtres
Effacer les filtres

How to write a code for varying step size ?

15 vues (au cours des 30 derniers jours)
revathy M
revathy M le 26 Sep 2017
Commenté : Janet onoja le 4 Avr 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 commentaire
James Tursa
James Tursa le 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?

Connectez-vous pour commenter.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 27 Sep 2017
Modifié(e) : Andrei Bobrov le 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 commentaires
revathy M
revathy M le 27 Sep 2017
Thank you.
Andrei Bobrov
Andrei Bobrov le 29 Sep 2017
Hi Revathy!
If this answer solve problem from your question, please, "accept" him.

Connectez-vous pour commenter.

Plus de réponses (3)

James Tursa
James Tursa le 26 Sep 2017
Modifié(e) : James Tursa le 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 commentaires
revathy M
revathy M le 27 Sep 2017
Hi! Thank you. Will try .
revathy M
revathy M le 27 Sep 2017
Hi! What's the difference between the above two syntax?

Connectez-vous pour commenter.


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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Nonlinear Optimization dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by