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

조회 수: 3 (최근 30일)
Mark Loui
Mark Loui 2021년 3월 26일
댓글: Mark Loui 2021년 3월 26일
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