error with inline function , why?

%Q3: Secant Method
close all;
clear all;
clc;
syms x;
fun = input('Give a function in x: ','s');
f = inline(fun,'x');
prompt = 'p0= ';
p0 = input(prompt);
prompt = 'p1= ';
p1 = input(prompt);
prompt= 'Enter the accuracy: ';
acc=input(prompt);
result = findRoot(f,p0,p1,acc);
fprintf('\n• The root of the equation using Secant method is = %.8f',result);
function r = findRoot(f,p0,p1,acc)
errors=[];
N=[];
Pn=[];
i=0;
sprintf('Iteration\t\tPn\t\t\tError')
p2=p1-((f(p1)*(p1-p0))/(f(p1)-f(p0)));
while(abs(p2-p1)>acc)
p0=p1;
p1=p2;
p2=p1-((f(p1)*(p1-p0))/(f(p1)-f(p0)));
err=abs(p2-p1);
errors(end+1)=err; i=i+1; N(end+1)=i;
Pn(end+1)=p1;
fprintf('\t\t\t%.0f\t\t%.8f\t%.8f\n',i,p1,err);
end
fprintf('\n• Number of iteration = %.0f',i);
r=p2;
%Error plot:
figure(1)
stem(N,errors)
xlim([0. 6])
title('Error Plot Secant method')
xlabel('n')
ylabel('|E|')
%Pn plot:
figure(2)
stem(N,Pn)
xlim([0. 6])
title('Pn -> P Plot Secant method')
xlabel('n')
ylabel('Pn')
end

댓글 수: 3

Walter Roberson
Walter Roberson 2022년 3월 10일
What error message are you observing?
Rik
Rik 2022년 3월 10일
I though you had copied the code on mobile and that is why your question lacks formatting. But no, you simply dumped this block of unformatted code here, expecting us to sort it out. Please do that first yourself.
Have a read here and here. It will greatly improve your chances of getting an answer.
Steven Lord
Steven Lord 2022년 3월 10일
Please stop using inline. Use anonymous functions (potentially in conjunction with str2func) instead.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Function Creation에 대해 자세히 알아보기

질문:

2022년 3월 10일

편집:

2022년 3월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by