MATLAB Answers

Can someone tell me what is wrong with this?

3 ビュー (過去 30 日間)
Ewa Jablonska
Ewa Jablonska 2019 年 11 月 13 日
回答済み: Ewa Jablonska 2019 年 11 月 13 日
x1=5;
tau=0.1;
k=1;
while x(k)-x(k+1)>0.01
k=k+1;
x(k+1)=x(k)-tau*(2*x(k)-2);
end
figure(1)
plot(x,'b*-.');
grid;

  6 件のコメント

表示 3 件の古いコメント
Ewa Jablonska
Ewa Jablonska 2019 年 11 月 13 日
That's the task and I have it using for loop, but I can not figure out how to do it with while loop.
12.png
M
M 2019 年 11 月 13 日
Why do you want it to work with a while loop ?
To see a comparison between for and while loops, you can have a look here:
Ewa Jablonska
Ewa Jablonska 2019 年 11 月 13 日
Because our lecturer said we should figure it out with while loop. :/

サインインしてコメントする。

採用された回答

Ewa Jablonska
Ewa Jablonska 2019 年 11 月 13 日
Thanks for effort, i mixed up + with -. :/ Correct:
%Value of step coefficient tau=0.1
x(1)=5;
k=1;
tau=0.1;
while x(k)>1.01
k=k+1;
x(k+1)=x(k)-tau*(2*x(k)-2);
end
plot(x,'b*-');
grid;

  0 件のコメント

サインインしてコメントする。

その他の回答 (3 件)

Fangjun Jiang
Fangjun Jiang 2019 年 11 月 13 日
The problem is, vector x is not defined. When you do x1=5, I think you meant x(1)=5. But even with that, when k=1, x(2) is not defined yet.
The solution is to add these lines at the begining.
N=100;x=zeros(N,1);x(1)=5;

  1 件のコメント

Ewa Jablonska
Ewa Jablonska 2019 年 11 月 13 日
%Value of step coefficient tau=0.1
x(1)=5;
k=1;
tau=0.1;
while x(k)>0.1
k=k+1;
x(k+1)=x(k)+tau*(2*x(k)+2);
end
plot(x,'b*-')
error:
Index exceeds array bounds.
Error in zad12p (line 7)
x(k+1)=x(k)+tau*(2*x(k)+2);

サインインしてコメントする。


David Hill
David Hill 2019 年 11 月 13 日
function x = Minimum(tau,xx)
x(1)=xx;
x(2)=x(1)-tau*(2*x(1)-2);
k=2;
while abs(x(k)-x(k-1))>0.01
x(k+1)=x(k)-tau*(2*x(k)-2);
k=k+1;
end
end

  2 件のコメント

Ewa Jablonska
Ewa Jablonska 2019 年 11 月 13 日
and how to see the result?
David Hill
David Hill 2019 年 11 月 13 日
you can see x in the workspace,
type 'disp(x)' and return,
or just type 'x' and return.

サインインしてコメントする。


Steven Lord
Steven Lord 2019 年 11 月 13 日
I'm guessing from the fact that you define x1 but don't use it that the x variable may not exist. You may have been thinking / hoping that MATLAB would use x1, x2, etc. when it saw x(1), x(2), etc. but it does not.
The fact that you're using x(k+1) (not x(k-1)) in your while condition also smells a bit strange. You may be walking off the end of the x array.
But if correcting these does not resolve the problem, please state more clearly the specific difficulty you're experiencing.
Do you receive an error message (text displayed in red)? If so, tell us on which line the error occurs and show us the full and exact text of the error, all the text displayed in red exactly as it's displayed in the Command Window.
Do you receive a warning (text displayed in orange)? If so, do the same as for an error but show us all the text displayed in orange.
Do you get a different answer than you expected? If so, show us the answer you received and the answer you expected and explain why you expected what you expected.
Does something else happen? If so, what?

  1 件のコメント

Ewa Jablonska
Ewa Jablonska 2019 年 11 月 13 日
%Value of step coefficient tau=0.1
x(1)=5;
k=1;
tau=0.1;
while x(k)>0.1
k=k+1;
x(k+1)=x(k)-tau*(2*x(k)+2);
end
plot(x,'b*-')
error: Index exceeds array bounds.
Error in zad12p (line 7)
x(k+1)=x(k)-tau*(2*x(k)+2);
No oragne just red.
Tasks given and must use while loop, because we did for "for loop":
12.png
The ponit is that function should end at specific value but it does not even with higher go to value.

サインインしてコメントする。

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by