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

2 vues (au cours des 30 derniers jours)
Mark Loui
Mark Loui le 18 Mar 2021
Commenté : Jan le 19 Mar 2021
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.

Réponse acceptée

John D'Errico
John D'Errico le 18 Mar 2021
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 commentaires
Walter Roberson
Walter Roberson le 19 Mar 2021
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 le 19 Mar 2021
@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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by