필터 지우기
필터 지우기

Function calling: Can someone help me in figuring out why is the main function not runing ?

조회 수: 1 (최근 30일)
I have created 3 functions 3 different files. every thing i try to run from main it gives me following error:
Can someone help me in figuring out why is the main function not runing ? I post my files below.
main
Attempt to execute SCRIPT main as a function:
/MATLAB Drive/main.m
Error in main (line 1)
main file
%main function
main file
num = input('Enter the number you want to find the square root: ');
sqroot(num);
sqroot.m
% Sqrt Calculator Function_____________
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 newton-raphson method 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;
err_est = nr-xn;
tru_err_arr =[tru_err_arr, tru_err];
est_err_arr =[est_err_arr, err_est];
%error check
if abs(err_est/xn)<esp
converge = true;
end
%disp([iter, xn, err_est, tru_err]);
%Iteration counter
iter_arr = [iter_arr, iter];
iter = (iter+1);
xn=nr;
end
sq_root = xn;
disp(['The sqrt of ', num2str(num),' is: ' ])
disp(sq_root);
info_plot(tru_err_arr, est_err_arr, iter_arr)
end
%Ploting function________________
info_plot.m
function info_plot(tru_err_arr, est_err_arr, iter_arr)
figure(1)
plot (iter_arr, abs(tru_err_arr), 'linewidth',2)
hold on
plot(iter_arr,abs(est_err_arr),'linewidth',2)
xlabel('iteration')
ylabel('Errors')
legend('True Error','Estimated Error')
title('Errors vs Graph')

답변 (1개)

Mario Malic
Mario Malic 2020년 9월 12일
As you can see - Error in main (line 1)
Script should be working if you remove it
main file
  댓글 수: 3
Mario Malic
Mario Malic 2020년 9월 12일
This error is also easy, as it says, all functions must be closed with an end. You're missing an end in one of your functions.

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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by