Function functions of fixed-point iteration
Afficher commentaires plus anciens
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 commentaires
KALYAN ACHARJYA
le 18 Mai 2019
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
le 18 Mai 2019
@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
le 18 Mai 2019
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
le 18 Mai 2019
I am sure, this doesnot work
[t]=F[@fun, x0,n]
madhan ravi
le 18 Mai 2019
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)
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Code Performance dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!