Why is this output nothing and what can i change to make it work

조회 수: 2 (최근 30일)
Mark Loui
Mark Loui 2021년 3월 18일
댓글: Jan 2021년 3월 19일
n=20;
x=rand(n,1)
y=rand(n,1);
RHS_empty=ones(n:1)
iter_h=0
iter_a=0;
for i=1:n-1
h(i)=x(i+1)-x(i);
iter_h=iter_h+1;
a(i)=y(i);
iter_a=iter_a+1;
RHS=a.*RHS_empty;
while n==4
SPLS=[1 0 0 0;0 h(1) 2*(h(2)+h(1)) h(2); 0 0 h(2) 2*(h(3)+h(2));0 0 0 h(3)]
c(:,i)=SPLS^-1*a(:,i)
end
% while n == 20
% SPLS=
%
% end
end
There is nothing wrong with the code in MATLAB where no red line was shown but the output was not shown for the SPLS and the c in the command window, can i know why and how can i change on this as i have another while loop to put inside there where the n=20, another set of matrix will be form but was not inserted into it by me as it is very long.

채택된 답변

John D'Errico
John D'Errico 2021년 3월 18일
I had to laugh at your statement that there is nothing wrong with the code. That seems counter to the fact that it produces nothing by your own statement. What it should do I cannot guess, because we are not told what it should produce. The crystal ball is just so foggy today.
In a quick perusal, what I did find interesting was this code fragment:
while n==4
SPLS=[1 0 0 0;0 h(1) 2*(h(2)+h(1)) h(2); 0 0 h(2) 2*(h(3)+h(2));0 0 0 h(3)]
c(:,i)=SPLS^-1*a(:,i)
end
So most of your code runs inside that while loop. Now, remember n is a scalar variable, that has been set to 20 in the beginning of the code. No place in the code has n ever been modified, so n will NEVER take on the value 4.
Yet, the important part of your code runs ONLY when n==4.
Do you see a fundamental problem here?
  댓글 수: 6
Walter Roberson
Walter Roberson 2021년 3월 19일
n=10
a=2
while n>=3
a=a+1;
break %Can i use stop or is there a function that i can call to stop this section from going forward again
end
But that would be pointless, since you could instead do
n=10
a=2
if n>=3
a=a+1;
end
since you would only be doing the increment once.
Jan
Jan 2021년 3월 19일
@Loui Pinn Wern: Although you can use break also, it is more direct to use the condition for stopping. In your case, the loop is stopped when n < 3. See:
doc while
Please read Matlab's Onramp to learn the basics. This is more efficient than asking them in the forum.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by