Hello, I have problem with Loop structure for fixed point.
This is the problem, and i made code like this.
It only caculates 1 time and just stops. Which mean, it's not repeating my code, xb=x; and x=sqrt(1.8*x+2.5)
if i manually repeat xb=x; and x=sqrt(1.8*x+2.5) the answer is correct, however it's not repeating those code.
What's wrong about my loop and How to fix it?

5 commentaires

Rik
Rik le 16 Avr 2021
Please post your code as code. Also, why haven't you tried stepping through your code line by line to see what the variables are?
You can find guidelines for posting homework on this forum here. If you have trouble with Matlab basics you may consider doing the Onramp tutorial (which is provided for free by Mathworks). If your main issue is with understanding the underlying concept, you may consider re-reading the material you teacher provided and ask them for further clarification.
I hope @VBBV will not actually do as you ask and provide a working solution. If you hand that in as your own work you would be commiting academic fraud. Asking for help is fine, asking for solutions isn't.
Your question is asking for help, the comment you wrote was asking for a solution. I was not claiming you were doing something nefarious, I was simply pointing you to helpful guidelines and to a tutorial.
If you had actually stepped through your code, you would have realised the values of your variables:
x0=5;
x=sqrt(1.8*x0+2.5);
xb=x;
x=sqrt(1.8*x+2.5);
disp((x-xb)/x)
-0.1561
The solution to your problem is that you probably want to look at the absolute value of the error term.
형석 김
형석 김 le 16 Avr 2021
Modifié(e) : 형석 김 le 16 Avr 2021
@Rik i've actually put abs on the error, by the way thanks for the advice.
i'm not sure the only problem is the abs..
cause after i put abs, it was also littlebit different from the manually solved answer
Rik
Rik le 17 Avr 2021
How large is that difference? And what is the code you're currently using? And what results are you expecting?
형석 김
형석 김 le 17 Avr 2021
Modifié(e) : 형석 김 le 17 Avr 2021
@Rik The value of x i got is 2.7892, and the correct answer when I calculated manually is 2.7424. I've also calculated error when x=2.7892 manually, but error sllightly exceeds 0.05, which is (2.9333-2.7892)/2.78792=0.05166.

Connectez-vous pour commenter.

 Réponse acceptée

Walter Roberson
Walter Roberson le 17 Avr 2021
and the correct answer when I calculated manually is 2.7424
No it is not. The correct answer is 2.71934053986603
format long g
roots([-1 1.8 2.5])
You tried the transform
-x^2 + 1.8*x + 2.5 == 0
implies 1.8*x + 2.5 == x^2
implies sqrt(1.8*x + 2.5) == sqrt(x^2)
and you deduced from that that
x == sqrt(1.8*x + 2.5)
but that is not correct. sqrt(x^2) is not x: it is abs(x) . So
abs(x) == sqrt(1.8*x + 2.5)
and you should be working both branches of that.
I suggest that you instead
-x^2 + 1.8*x + 2.5 == 0
implies 1.8*x == x^2 - 2.5
implies x == (x^2 - 2.5)/1.8

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by