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 Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by