I am trying to calculate the error of a function within some limit, but I am getting wrong answer. I have attached my code. Please help me to find out the problem. Thanx
clear all
clc
x=1;
err=10000;
while (err>=20)
err=131-x^2;
x=x+1;
end
After running the code, err is coming out less than 20. Please help me to fix it.

6 Kommentare

What do you expect?
x=1;
err=10000;
while (err>=20)
err=131-x^2;
[x, err]
x=x+1;
end
ans = 1×2
1 130
ans = 1×2
2 127
ans = 1×2
3 122
ans = 1×2
4 115
ans = 1×2
5 106
ans = 1×2
6 95
ans = 1×2
7 82
ans = 1×2
8 67
ans = 1×2
9 50
ans = 1×2
10 31
ans = 1×2
11 10
SANDIPKUMAR ROYADHIKARI
SANDIPKUMAR ROYADHIKARI am 28 Aug. 2021
I am expecting the code should run until err=>20 but in my code runs till err 10, that's my problem is . Please reply . Thanx
Image Analyst
Image Analyst am 28 Aug. 2021
With the formula you chose, it never attains that particular number, 20. It goes from 67 to 50 to 31, and then finally to 10 when it will exit the loop. Why do you think it should hit 20 EXACTLY? It just won't.
SANDIPKUMAR ROYADHIKARI
SANDIPKUMAR ROYADHIKARI am 28 Aug. 2021
I don't want err to be exactly 20 but I want err to be greater or equal to 20 . So I took err=> 20 , so that it should give at 20 or great than 20 but the answer is coming less than 20, that's the problem.
Walter Roberson
Walter Roberson am 28 Aug. 2021
The error would be greater than 20 at your starting point, so why not stop there?
Are you trying to find the last i that gives err>20 rather than the first for which it is less? If so then after your loop, subtract off the last increment from i. You are adding 1 to i each time so subtract 1 from i.
SANDIPKUMAR ROYADHIKARI
SANDIPKUMAR ROYADHIKARI am 28 Aug. 2021
Are you saying this ?
x=1;
err=1000;
while err >20
err=131-x^2;
x=x-1;
end
This also doesn't give the correct answer

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Awais Saeed
Awais Saeed am 28 Aug. 2021
Bearbeitet: Awais Saeed am 28 Aug. 2021

0 Stimmen

You wrote a loop to run when err >= 20, its value is decreasing in the loop and when err<20 (in your case 10), why would it still run? It has to stop. See the output to know the reason
x=1;
err=10000;
while (err>=20)
err=131-x^2;
fprintf('x = %d, err = %d\n', x, err)
x=x+1;
end
x = 1, err = 130
x = 2, err = 127
x = 3, err = 122
x = 4, err = 115
x = 5, err = 106
x = 6, err = 95
x = 7, err = 82
x = 8, err = 67
x = 9, err = 50
x = 10, err = 31
x = 11, err = 10
Walter Roberson
Walter Roberson am 28 Aug. 2021

0 Stimmen

You are looking for an integer, x, such that
syms x
xsol = solve(131-x^2 == 20)
xsol = 
You can see from xsol that the x that solve that equation are not integers: they are numbers that are between 10 and 11 and the negative of that.
Changing to a smaller increment such as 0.1 will only get you closer . No matter what rational increment you use instead of 1, the actual solutions are irrational and so cannot be reached by the method you are using (though possibly you could find something that came out okay to within roundoff error

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by