f(x) = e^x-3x matlab code
์ด์ ๋๊ธ ํ์
I want to write a matlab script that finds solutions of function f(x) = e^x - 3x. I tried to write some code. My matlab code should include iteration table and graphic of function. Can anyone help me please with a code or give me advices?
f=@(x) exp(x) - 3*x ;
a = 0; b = 1;tol = 1e-8;
if (f(a)*f(b) > 0)
error ('invalid choice of interval')
end
r=0; n=0;
while ((b-a)/2 > 0)
n = n + 1;
r = (a+b)/2;
if (f(r) == 0)
break;
elseif (f(a)*f(r) <= 0)
b = r ;
else
a = r;
end
end
๋๊ธ ์: 3
Matt J
2018๋
2์ 25์ผ
solutions of function f(x) = e^x - 3x
I think what you really mean is, solutions of the equation
exp(x) - 3*x = 0
Adomas Bazinys
2018๋
2์ 25์ผ
Rik
2018๋
2์ 25์ผ
Did you read Steven Lord's comment? Your current code finds 1 solution to this equation already.
If you have some restriction (e.g. because this is a homework assignment), please mention them.
์ฑํ๋ ๋ต๋ณ
์ถ๊ฐ ๋ต๋ณ (2๊ฐ)
Rik
2018๋
2์ 25์ผ
If you want to store intermediate result, us the n for indexing. Also, don't you mean if abs(f(r(n)))<=tol? Then you would actually use the variable tol.
f=@(x) exp(x) - 3*x ;
a = 0; b = 1;tol = 1e-8;
if (f(a)*f(b) > 0)
error ('invalid choice of interval')
end
r=0; n=0;
while ((b-a)/2 > 0)
n = n + 1;
r(n) = (a+b)/2;%#ok suppress warning, we can't know the length of r in advance
if (f(r(n)) == 0)
%if abs(f(r(n)))<=tol
break;
elseif (f(a)*f(r(n)) <= 0)
b = r(n) ;
else
a = r(n);
end
end
it_table=[r' f(r)'];
clc
disp(it_table)
figure(1),clf(1)
plot(1:numel(r),f(r))
xlabel('Iteration number'),ylabel('f(r)')
maryam
2022๋
10์ 25์ผ
0 ๊ฐ ์ถ์ฒ
Find an approximation to within 0.00001 to a value in [0, 1] with ๐(๐ฅ) = ๐ ๐ฅ โ ๐ฅ 2 + 3๐ฅ โ 2 = 0 Use Bisection method
๋๊ธ ์: 1
Walter Roberson
2022๋
10์ 25์ผ
No, that will certainly not solve the question posted by Adomas
์นดํ ๊ณ ๋ฆฌ
๋์๋ง ์ผํฐ ๋ฐ File Exchange์์ Logical์ ๋ํด ์์ธํ ์์๋ณด๊ธฐ
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
