How can I input values?

1 次查看(过去 30 天)
Melchor
Melchor 2023-5-11
syms x;
f = input('Type the function expression, f: ');
xi = input('Enter the initial estimate, x0: ');
tol = input('Tolerable error: ');
disp(' Results of Newton-Raphson Iteration')
disp('=================================================================')
fprintf('k\txi\t\txr\t\tf(xi)\t\tder(xi)\n');
i = 1000;
k = 1;
g = diff(f,x);
fa = eval(subs(f,x,xi));
while abs(fa)> tol
fa = eval(subs(f,x,xi));
ga = eval(subs(g,x,xi));
b = xi - fa/ga;
fprintf('%d\t%f\t%f\t%f\t%f\n',k,xi,b,fa,ga);
xi = b;
k = k + 1;
end
fprintf('Root is %f\n', xi);
f=@(x)exp(x)+23*x;
xint=[2 5];
tol=1e-6;
perf='max';
[iter,x,fval]=goldensection(f,xint,tol,perf)
-----------------------------
function [iter,root]=falsepos(f,x,tol)
format short g
if(f(x(1))*f(x(2)))>0
error('Your initial values are incorrect! Cannot find root within the interval.')
else
k=1;
xr=(x(1)+x(2))/2;
iter(1,1:7)=[k x xr f(x(1)) f(x(2)) f(xr)];
while(abs(iter(k,7))>tol)
k=k+1;
if (iter(k-1,7)*iter(k-1,5))<0
iter(k,3)=iter(k-1,4);
iter(k,6)=iter(k-1,7);
iter(k,2)=iter(k-1,2);
iter(k,5)=iter(k-1,5);
else
iter(k,2)=iter(k-1,4);
iter(k,5)=iter(k-1,7);
iter(k,3)=iter(k-1,3);
iter(k,6)=iter(k-1,6);
end
iter(k,4)=(iter(k,2)+iter(k,3))/2;
iter(k,7)=f(iter(k,4));
iter(k,1)=k;
end
root=iter(k,4);
end
end
f=@(x) exp(x)-3*x;
x=[0 1];
tol = 1e-5;
[iter,root]=falsepos(f,x,tol)
or (bisect)
k=1;
xr=(x(1)+x(2))/2;
iter(1,1:7)=[k x xr f(x(1)) f(x(2)) f(xr)];
while(abs(iter(k,7))>tol)
k=k+1;
if (iter(k-1,7)*iter(k-1,5))<0
iter(k,3)=iter(k-1,4);
iter(k,6)=iter(k-1,7);
iter(k,2)=iter(k-1,2);
iter(k,5)=iter(k-1,5);
else
iter(k,2)=iter(k-1,4);
iter(k,5)=iter(k-1,7);
iter(k,3)=iter(k-1,3);
iter(k,6)=iter(k-1,6);
end
iter(k,4)=(iter(k,2)+iter(k,3))/2;
iter(k,7)=f(iter(k,4));
iter(k,1)=k;
end
root=iter(k,4);
end
end
f=@(x) exp(x)-3*x;
x=[0 1];
tol = 1e-5;
[iter,root]=bisection(f,x,tol)
  2 个评论
Walter Roberson
Walter Roberson 2023-5-11
You show a function named falsepos but you call a function named goldensection ??
Walter Roberson
Walter Roberson 2023-5-11
fa = eval(subs(f,x,xi));
You should never eval() the result of subs(). MATLAB has no documented meaning for eval() of a symbolic expression. You should either use vpa() or double() instead of eval(), depending what you are trying to do.

请先登录,再进行评论。

回答(1 个)

Sulaymon Eshkabilov
You code contains several crucial errors including wrong call of fcn, wrong placements of commands after the fcn file, wrong input variables and output variables, calling inexistance or not yet defined fcns (bisection), etc. Here is the corrected code (maybe still a few edits are necessary). It runs ok, computes the solutions and shows all iteration values. It can be tested with e.g.:
Input 1. ... f: @ @(x) exp(x)-3*x
Input 2. ... : [0, 1]
Input 3. ... : 1e-1
syms x;
f = input('Type the function expression, f: ');
xi = input('Enter the initial estimate, x0: ');
tol = input('Tolerable error: ');
disp(' Results of Newton-Raphson Iteration')
disp('=================================================================')
fprintf('k\txi\t\txr\t\tf(xi)\t\tder(xi)\n');
i = 1000;
k = 1;
g = diff(f,x);
fa = double(f(xi));
f=@(x)exp(x)-3*x;
x=[0 1];
tol = 1e-5;
[iter,root]=falsepos(f,x,tol)
f=@(x) exp(x)-3*x;
x=[0 1];
tol = 1e-5;
%[iter,root]=bisection(f,x,tol)
while abs(fa)> tol
fa = double(f(xi));
ga = double(subs(g,xi));
b = xi - fa/ga;
fprintf('%d\t%f\t%f\t%f\t%f\n',k,xi,b,fa,ga);
xi = b;
k = k + 1;
end
fprintf('Root is %f\n', xi);
xr=(x(1)+x(2))/2;
iter(1,1:7)=[k x xr f(x(1)) f(x(2)) f(xr)];
k=1;
while(abs(iter(k,7))>tol)
k=k+1;
if (iter(k-1,7)*iter(k-1,5))<0
iter(k,3)=iter(k-1,4);
iter(k,6)=iter(k-1,7);
iter(k,2)=iter(k-1,2);
iter(k,5)=iter(k-1,5);
else
iter(k,2)=iter(k-1,4);
iter(k,5)=iter(k-1,7);
iter(k,3)=iter(k-1,3);
iter(k,6)=iter(k-1,6);
end
iter(k,4)=(iter(k,2)+iter(k,3))/2;
iter(k,7)=f(iter(k,4));
iter(k,1)=k;
end
root=iter(k,4);
f=@(x)exp(x)+23*x;
xint=[2 5];
tol=1e-6;
perf='max';
[iter,x]=falsepos(f,xint,tol)
%-----------------------------
function [iter,root]=falsepos(f,x,tol)
format short g
if(f(x(1))*f(x(2)))>0
error('Your initial values are incorrect! Cannot find root within the interval.')
else
k=1;
xr=(x(1)+x(2))/2;
iter(1,1:7)=[k x xr f(x(1)) f(x(2)) f(xr)];
while(abs(iter(k,7))>tol)
k=k+1;
if (iter(k-1,7)*iter(k-1,5))<0
iter(k,3)=iter(k-1,4);
iter(k,6)=iter(k-1,7);
iter(k,2)=iter(k-1,2);
iter(k,5)=iter(k-1,5);
else
iter(k,2)=iter(k-1,4);
iter(k,5)=iter(k-1,7);
iter(k,3)=iter(k-1,3);
iter(k,6)=iter(k-1,6);
end
iter(k,4)=(iter(k,2)+iter(k,3))/2;
iter(k,7)=f(iter(k,4));
iter(k,1)=k;
end
root=iter(k,4);
end
end

类别

Help CenterFile Exchange 中查找有关 Equation Solving 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by