Filter löschen
Filter löschen

I'm trying to run a loop until a condition is met

1 Ansicht (letzte 30 Tage)
Bruce Griffin
Bruce Griffin am 25 Okt. 2022
Kommentiert: Image Analyst am 26 Okt. 2022
I want to run a for loop until my slope of a line is equal to another number:
I have a secant line between two points and I'm calculating every tangent line starting from one point to another.
I can't seem to end the loop when the tangent line slope equals the secant line
clear all
a=-2;
b=8;
x=linspace(-10,10,100);
y=x.^3;
yb=b.^2;
ya=a.^2;
msecant=(yb-ya)/(b-a);
secant=msecant*(x-a)+ya;
for i=1:100
mt=3*x(i)^2;
inter =1;
if abs(mt-msecant)>.4
yt=mt*(x-x(i))+y(i);
else
return
end
end
  2 Kommentare
Torsten
Torsten am 25 Okt. 2022
Your code ?
Torsten
Torsten am 25 Okt. 2022
I don't understand what your code is supposed to do.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

John D'Errico
John D'Errico am 25 Okt. 2022
Bearbeitet: John D'Errico am 25 Okt. 2022
Simple rule #1: A for loop is best when you KNOW how many iterations you wish to perform. How many times do you want it to loop? If you know the answer in advance, then use a for loop.
Simple rule #2: Use a while loop when you don't know how many iterations you will perform. It has a test in there, to allow the loop to gracefully terminate.
In some cases, you may decide that you want to perform N iterations, BUT if some condition is met, then you are willing to terminate early. Then you might decide to choose between the two programming styles. Either could be appropriate.
The last point is that every rule has an exception. You can use the break statement to exit a for loop when something special happens. But you can just as easily set it up with a while loop, incrementing a counter, and then terminate if either the counter exceeds N, OR if the termination condition is met. Your choice.

Image Analyst
Image Analyst am 25 Okt. 2022
They never get within 0.4 of each other. I suggest you alter your ending condition. See this
clear all
a=-2;
b=8;
x=linspace(-10,10,100);
y=x.^3;
yb=b.^2;
ya=a.^2;
msecant=(yb-ya)/(b-a);
secant=msecant*(x-a)+ya;
for i=1:100
fprintf('i = %d', i)
mt=3*x(i)^2;
inter =1;
fprintf(' mt = %.2f. msecant = %.2f\n', mt, msecant)
if abs(mt-msecant)>.4
yt=mt*(x-x(i))+y(i);
else
return
end
end
i = 1
mt = 300.00. msecant = 6.00
i = 2
mt = 288.00. msecant = 6.00
i = 3
mt = 276.25. msecant = 6.00
i = 4
mt = 264.74. msecant = 6.00
i = 5
mt = 253.47. msecant = 6.00
i = 6
mt = 242.45. msecant = 6.00
i = 7
mt = 231.68. msecant = 6.00
i = 8
mt = 221.15. msecant = 6.00
i = 9
mt = 210.87. msecant = 6.00
i = 10
mt = 200.83. msecant = 6.00
i = 11
mt = 191.03. msecant = 6.00
i = 12
mt = 181.48. msecant = 6.00
i = 13
mt = 172.18. msecant = 6.00
i = 14
mt = 163.12. msecant = 6.00
i = 15
mt = 154.30. msecant = 6.00
i = 16
mt = 145.73. msecant = 6.00
i = 17
mt = 137.40. msecant = 6.00
i = 18
mt = 129.32. msecant = 6.00
i = 19
mt = 121.49. msecant = 6.00
i = 20
mt = 113.90. msecant = 6.00
i = 21
mt = 106.55. msecant = 6.00
i = 22
mt = 99.45. msecant = 6.00
i = 23
mt = 92.59. msecant = 6.00
i = 24
mt = 85.98. msecant = 6.00
i = 25
mt = 79.61. msecant = 6.00
i = 26
mt = 73.49. msecant = 6.00
i = 27
mt = 67.62. msecant = 6.00
i = 28
mt = 61.98. msecant = 6.00
i = 29
mt = 56.60. msecant = 6.00
i = 30
mt = 51.45. msecant = 6.00
i = 31
mt = 46.56. msecant = 6.00
i = 32
mt = 41.90. msecant = 6.00
i = 33
mt = 37.50. msecant = 6.00
i = 34
mt = 33.33. msecant = 6.00
i = 35
mt = 29.42. msecant = 6.00
i = 36
mt = 25.74. msecant = 6.00
i = 37
mt = 22.31. msecant = 6.00
i = 38
mt = 19.13. msecant = 6.00
i = 39
mt = 16.19. msecant = 6.00
i = 40
mt = 13.50. msecant = 6.00
i = 41
mt = 11.05. msecant = 6.00
i = 42
mt = 8.85. msecant = 6.00
i = 43
mt = 6.89. msecant = 6.00
i = 44
mt = 5.17. msecant = 6.00
i = 45
mt = 3.70. msecant = 6.00
i = 46
mt = 2.48. msecant = 6.00
i = 47
mt = 1.50. msecant = 6.00
i = 48
mt = 0.77. msecant = 6.00
i = 49
mt = 0.28. msecant = 6.00
i = 50
mt = 0.03. msecant = 6.00
i = 51
mt = 0.03. msecant = 6.00
i = 52
mt = 0.28. msecant = 6.00
i = 53
mt = 0.77. msecant = 6.00
i = 54
mt = 1.50. msecant = 6.00
i = 55
mt = 2.48. msecant = 6.00
i = 56
mt = 3.70. msecant = 6.00
i = 57
mt = 5.17. msecant = 6.00
i = 58
mt = 6.89. msecant = 6.00
i = 59
mt = 8.85. msecant = 6.00
i = 60
mt = 11.05. msecant = 6.00
i = 61
mt = 13.50. msecant = 6.00
i = 62
mt = 16.19. msecant = 6.00
i = 63
mt = 19.13. msecant = 6.00
i = 64
mt = 22.31. msecant = 6.00
i = 65
mt = 25.74. msecant = 6.00
i = 66
mt = 29.42. msecant = 6.00
i = 67
mt = 33.33. msecant = 6.00
i = 68
mt = 37.50. msecant = 6.00
i = 69
mt = 41.90. msecant = 6.00
i = 70
mt = 46.56. msecant = 6.00
i = 71
mt = 51.45. msecant = 6.00
i = 72
mt = 56.60. msecant = 6.00
i = 73
mt = 61.98. msecant = 6.00
i = 74
mt = 67.62. msecant = 6.00
i = 75
mt = 73.49. msecant = 6.00
i = 76
mt = 79.61. msecant = 6.00
i = 77
mt = 85.98. msecant = 6.00
i = 78
mt = 92.59. msecant = 6.00
i = 79
mt = 99.45. msecant = 6.00
i = 80
mt = 106.55. msecant = 6.00
i = 81
mt = 113.90. msecant = 6.00
i = 82
mt = 121.49. msecant = 6.00
i = 83
mt = 129.32. msecant = 6.00
i = 84
mt = 137.40. msecant = 6.00
i = 85
mt = 145.73. msecant = 6.00
i = 86
mt = 154.30. msecant = 6.00
i = 87
mt = 163.12. msecant = 6.00
i = 88
mt = 172.18. msecant = 6.00
i = 89
mt = 181.48. msecant = 6.00
i = 90
mt = 191.03. msecant = 6.00
i = 91
mt = 200.83. msecant = 6.00
i = 92
mt = 210.87. msecant = 6.00
i = 93
mt = 221.15. msecant = 6.00
i = 94
mt = 231.68. msecant = 6.00
i = 95
mt = 242.45. msecant = 6.00
i = 96
mt = 253.47. msecant = 6.00
i = 97
mt = 264.74. msecant = 6.00
i = 98
mt = 276.25. msecant = 6.00
i = 99
mt = 288.00. msecant = 6.00
i = 100
mt = 300.00. msecant = 6.00
  2 Kommentare
Bruce Griffin
Bruce Griffin am 25 Okt. 2022
actually yeah thats the next thing im having to conquer here. do you have any suggestions on how to determine the end conditions. i know the odds of my mt and msecant being clode is determined by my step size. but im trying to write a general code. Everytime i change my function i have to retry a new threshold.
Image Analyst
Image Analyst am 26 Okt. 2022
Make x have more elements, like a million or so. Of course in that case, don't use fprintf() or it will take forever.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help 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