필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Im trying to create two new file function base that will get call in my sqroot function. Im getting all types of errors when i try implementing

조회 수: 2 (최근 30일)
My code is below the 2 equations im trying to make them into 2 separate file functions are in line 39 and 40. f = x^2-num and df = 2*x
function [sq_root] = sqroot(num)
format long
converge = false;
iter = 0;
%Max allow of error
esp = 10^-10;
%intial guess
xn=1;
%disp('iter, x_n, err_est; err_exact');
tru_err_arr = [];
est_err_arr = [];
iter_arr = [];
%Using imporove initial value to find sqrt
if num >= 1
xn =1;
while xn^2<num
xn = xn+10;
end
xn = xn/2;
else
xn =1;
while xn^2>num
xn = xn/10;
end
xn = xn*2;
end
while converge == false
%Function definations
f = xn^2-num;
df = 2*xn;
%newton_raphson
nr = xn -(f/df);
%error defination
tru_err = sqrt(num)-xn;
est_err = nr-xn;
tru_err_arr =[tru_err_arr, tru_err];
est_err_arr =[est_err_arr, est_err];
%error check
if abs(est_err/xn)<esp
converge = true;
end
disp([iter, xn,est_err, tru_err]);
%Iteration counter
iter_arr = [iter_arr, iter];
iter = (iter+1);
xn=[];
xn=nr;
%----attemp to export to excel------------
R0 = {'iter','xn', 'est_err', 'tru_err'};
R1 = iter_arr;
R2 = xn;
R3 = est_err_arr;
R4 = tru_err_arr;
xlswrite('results.xlsx', R0,'Sheet1','A1');
xlswrite('results.xlsx', R1,'Sheet1','A2');
xlswrite('results.xlsx', R2,'Sheet1','B2');
xlswrite('results.xlsx', R3,'Sheet1','C2');
%-----------------------------------------------
end
sq_root = xn;
disp('Determining the square root of:');
disp(num);
disp(['The sqrt of ', num2str(num),' is: ' ]);
disp(sq_root);
disp('Number of iteration taken:');
disp(iter-1);
info_plot(tru_err_arr, est_err_arr, iter_arr);
end

답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 9월 16일
Do it like this.
Create a file name fun1.m and write following in it
function y = fun1(xn, num)
f = xn^2-num;
end
Then create fun2.m and write
function y = fun2(xn)
f = 2*xn;
end
and then create file sqroot.m and write
function [sq_root] = sqroot(num)
format long
converge = false;
iter = 0;
%Max allow of error
esp = 10^-10;
%intial guess
xn=1;
%disp('iter, x_n, err_est; err_exact');
tru_err_arr = [];
est_err_arr = [];
iter_arr = [];
%Using imporove initial value to find sqrt
if num >= 1
xn =1;
while xn^2<num
xn = xn+10;
end
xn = xn/2;
else
xn =1;
while xn^2>num
xn = xn/10;
end
xn = xn*2;
end
while converge == false
%Function definations
f = fun1(xn, num);
df = fun2(xn);
%newton_raphson
nr = xn -(f/df);
%error defination
tru_err = sqrt(num)-xn;
est_err = nr-xn;
tru_err_arr =[tru_err_arr, tru_err];
est_err_arr =[est_err_arr, est_err];
%error check
if abs(est_err/xn)<esp
converge = true;
end
disp([iter, xn,est_err, tru_err]);
%Iteration counter
iter_arr = [iter_arr, iter];
iter = (iter+1);
xn=[];
xn=nr;
%----attemp to export to excel------------
R0 = {'iter','xn', 'est_err', 'tru_err'};
R1 = iter_arr;
R2 = xn;
R3 = est_err_arr;
R4 = tru_err_arr;
xlswrite('results.xlsx', R0,'Sheet1','A1');
xlswrite('results.xlsx', R1,'Sheet1','A2');
xlswrite('results.xlsx', R2,'Sheet1','B2');
xlswrite('results.xlsx', R3,'Sheet1','C2');
%-----------------------------------------------
end
sq_root = xn;
disp('Determining the square root of:');
disp(num);
disp(['The sqrt of ', num2str(num),' is: ' ]);
disp(sq_root);
disp('Number of iteration taken:');
disp(iter-1);
info_plot(tru_err_arr, est_err_arr, iter_arr);
end

이 질문은 마감되었습니다.

태그

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by