MATLAB Answers

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

1 view (last 30 days)
Fernando Piedra
Fernando Piedra on 12 Sep 2020
Commented: Mario Malic on 12 Sep 2020
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')

Answers (1)

Mario Malic
Mario Malic on 12 Sep 2020
As you can see - Error in main (line 1)
Script should be working if you remove it
main file
  3 Comments
Mario Malic
Mario Malic on 12 Sep 2020
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!