using while loop to solve a function

์กฐํšŒ ์ˆ˜: 4 (์ตœ๊ทผ 30์ผ)
caroline
caroline 2019๋…„ 1์›” 27์ผ
ํŽธ์ง‘: Stephen23 2019๋…„ 1์›” 27์ผ
Given the following function ?(x) = x^3 โˆ’ (5?)^2 + 2^x โˆ’ 10000x Use a while loop to compute f(x) for many evenly spaced values of 0 โ‰ค x โ‰ค 20 and create a plot of the results
my solution:
x = linspace(0,20,20);
j = 0;
while (j<=20)
y = x.^3-(5.*x).^2+2.^x-(10000.*x);
j = j+1;
plot(x,y)
end
WHEN I GRAPH ON MY CALCULATOR IT IS JUST A STRAIGHT LINE DOWN NOTHING LIKE MY PLOT. I'T SHOULD BE A STRAIGHT LINE , I GUESS IM JUST WONDERING IF MY SOLUTION AND GRAPH ARE CORRECT
  ๋Œ“๊ธ€ ์ˆ˜: 1
Stephen23
Stephen23 2019๋…„ 1์›” 27์ผ
ํŽธ์ง‘: Stephen23 2019๋…„ 1์›” 27์ผ
Note that your loop is entirely superfluous because you do not change anything inside that loop to change the calculation of y. Basically every loop iteration is discarded except for the last one.
You generate the entire x vector and wrote vectorized code to calculate y, which is a better way to solve this than what your assignment asked for (using a loop):
x = linspace(0,20,20);
y = x.^3-(5.*x).^2+2.^x-(10000.*x);
plot(x,y)
capture.png

๋Œ“๊ธ€์„ ๋‹ฌ๋ ค๋ฉด ๋กœ๊ทธ์ธํ•˜์‹ญ์‹œ์˜ค.

๋‹ต๋ณ€ (0๊ฐœ)

์นดํ…Œ๊ณ ๋ฆฌ

Help Center ๋ฐ File Exchange์—์„œ Loops and Conditional Statements์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ

ํƒœ๊ทธ

์ œํ’ˆ


๋ฆด๋ฆฌ์Šค

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by