using for loop to solve a function

์กฐํšŒ ์ˆ˜: 3 (์ตœ๊ทผ 30์ผ)
caroline
caroline 2019๋…„ 1์›” 27์ผ
๋Œ“๊ธ€: caroline 2019๋…„ 1์›” 28์ผ
Given the following function ??(??) = 20000 * log(x) โˆ’ 3x Use a for loop to compute f(x) for 1 โ‰ค x โ‰ค 20.
my solution
x = linspace(0,20,20);
for i=1:20
x2 = x;
y2 = 20000*log(x)-3*x;
plot(x2,y2,'r')
am i on the right track plot is blank

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

madhan ravi
madhan ravi 2019๋…„ 1์›” 27์ผ
ํŽธ์ง‘: madhan ravi 2019๋…„ 1์›” 27์ผ
I know it is your homwork and appreciate that you have tried something.
I suggest you to do Matlab Onramp course which is interactive and fun to learn MATLAB.
Loop version:
x = linspace(0,20,20);
% ^^------ increase to get a smoother curve
y2=zeros(size(x)); % preallocate
for k=1:numel(x)
% x2 = x; ?? does nothing
y2(k) = 20000*log(x(k))-3*x(k);
% ^^^------ save y2 in each iteration
end
plot(x,y2,'r') % plot outside the loop
Matlab's way (vectorization):
x = linspace(0,20,20); % just three lines of code
y2 = 20000*log(x)-3*x;
plot(x,y2,'r')
  ๋Œ“๊ธ€ ์ˆ˜: 11
madhan ravi
madhan ravi 2019๋…„ 1์›” 28์ผ
ํŽธ์ง‘: madhan ravi 2019๋…„ 1์›” 28์ผ
You didn't answer my first question.Plus I don't see any differences in the figure? not sure what do you mean by "i just thought it was wrong because when plotted on calc it looked wrong. " Did you verify the scale?
caroline
caroline 2019๋…„ 1์›” 28์ผ
original question was for loops yes

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

์นดํ…Œ๊ณ ๋ฆฌ

Help Center ๋ฐ File Exchange์—์„œ Graphics Performance์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ

ํƒœ๊ทธ

์ œํ’ˆ


๋ฆด๋ฆฌ์Šค

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by