Function functions of fixed-point iteration
이전 댓글 표시
Hello,
I'm trying to make function functions, but I have an error in the last row and I don't know that's wrong:
clc;
close all;
clear all;
syms x;
fun=@(x)-0.0641*x^3+1.0889*x^2-3.8260*x+0.6364;
n=input('Enter the number of decimal places:');
x0 = input('Enter the intial approximation:');
[t]=F[@fun, x0,n]
My function F is:
function [t] = F(fun, x0, n)
% Detailed explanation goes here
g=diff(fun);
epsilon = 5*10^-(n+1)
i=1;
rng(0,'twister');
alpha = -2/vpa(subs(g,x,x0));
x_current = x0;
while i~=200
phi_of_x_at_x_current= x_current + (alpha*vpa(subs(fun,x,x_current)));
err=abs(phi_of_x_at_x_current-x_current);
if abs(1+alpha*vpa(subs(g,x,x_current)))>=1
alpha = -1*(2/vpa(subs(g,x,x0))*rand);
i=1;
elseif err<epsilon
break
end
x_current = phi_of_x_at_x_current;
i=i+1;
end
phi_of_x_at_x_current = phi_of_x_at_x_current - rem(phi_of_x_at_x_current,10^-n);
fprintf('Answer: %f \n',phi_of_x_at_x_current);
end
댓글 수: 5
KALYAN ACHARJYA
2019년 5월 18일
Is this you are looking for?
syms x;
fun1=@(x)-0.0641*x^3+1.0889*x^2-3.8260*x+0.6364;
n=input('Enter the number of decimal places:');
x0 = input('Enter the intial approximation:');
[t]=F(fun1,x0,n);
Please note that I have changed the fun name to fun1
KALYAN ACHARJYA
2019년 5월 18일
@madhan ravi Comment
I think it would be better to remove @(x) Kalyan, what do you think? Because diff command doesn’t work for function handle unless evaluated.
KALYAN ACHARJYA
2019년 5월 18일
syms x;
fun1=-0.0641*x^3+1.0889*x^2-3.8260*x+0.6364;
n=input('Enter the number of decimal places:');
x0 = input('Enter the intial approximation:');
[t]=F(fun1,x0,n)
Result:
Enter the number of decimal places:2
Enter the intial approximation:3
epsilon =
0.0050
Undefined function or variable 'x'.
Error in F (line 7)
alpha = -2/vpa(subs(g,x,x0));
Error in ans_may19 (line 8)
[t]=F(fun1,x0,n)
>>
KALYAN ACHARJYA
2019년 5월 18일
I am sure, this doesnot work
[t]=F[@fun, x0,n]
madhan ravi
2019년 5월 18일
Haven’t checked the code perhaps:
fun1(x)=-0.0641*x^3+1.0889*x^2-3.8260*x+0.6364;
t=F(fun1(x),x0,n)
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Code Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!