Hi i need some help here to put this into a matlab

1 visualizzazione (ultimi 30 giorni)
Hi there i need some help please
How can i put these understanding into matlab form
while err<tol %I have other sets of code for this
the main thing is here
i like to put these understanding into a code form, without using a for loop as the while loop is to determine how many loops must it get
h=0.5;
a=0;
xnew=(a+h) %First
h=h/2
xnew2=(a+h a+2*h) %Second
h=h/2
xnew3=(a+h, a+2*h, a+4*h, a+6*h) %FOr every new xnew the element size is increase by multiple by 2 and the general form can be written as (a+h, a+2(n)*h) where n is 0,2,4,6,8
h=h/2
xnew4=(a+h, a+2*h, a+4*h, a+6*h, a+8*h, a+10*h, a+12*h, a+14*h)
h=h/2 %and the same set of code is to run until it satisfied the while loop
Please help and thank you in advance

Risposta accettata

KALYAN ACHARJYA
KALYAN ACHARJYA il 26 Mar 2021
h=0.5;
a=0;
x=a+h;
i=2;
tol=...?
while err<tol
x=[x a+i*h];
h=h/2;
i=i+2;
err=..% Update
end
  1 Commento
Mark Loui
Mark Loui il 26 Mar 2021
Hi there i cant call your codes as well, here are the codes i have used.
delete all;clear all;close all;
a=0;
b=1;
h=b-a;
x=a+(h/2);
i=2;
tol=1e-2;
err=0.1;
while err>tol
x(i)=[x a+i*h];
h=h/2;
i=i+2;
err=err-0.1;
end

Accedi per commentare.

Più risposte (1)

David Hill
David Hill il 26 Mar 2021
i=0;
while err<tol
xnew{i+1}=a+[h,h*2*(1:2^i-1)];%index into cell array (much better than different variable names)
h=h/2;
i=i+1;
err;%? not sure what you are doing here
end
  1 Commento
Mark Loui
Mark Loui il 26 Mar 2021
Hi there i tried your method, it does not output for the xnew variable,
a=0;
b=1;
h=b-a;
err=0.1;
tol=1e-2;
i=0;
while err < tol
h=h/2;
xnew{i+1}=a+[h,h*2*(1:2^i-1)]
i=i+1;
err=err-0.1;
end
here are the codes that i use for the situation

Accedi per commentare.

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by