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

3 次查看(过去 30 天)
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

采纳的回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2021-3-26
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 个评论
Mark Loui
Mark Loui 2021-3-26
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

请先登录,再进行评论。

更多回答(1 个)

David Hill
David Hill 2021-3-26
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 个评论
Mark Loui
Mark Loui 2021-3-26
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by