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

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')

Réponses (1)

As you can see - Error in main (line 1)
Script should be working if you remove it
main file

3 commentaires

Thanks! That work but my ploting functions is nit working. Now i got this error
Enter the number you want to find the square root:
32
The sqrt of 32 is:
5.656854249492381
Error: File: info_plot.m Line: 11 Column: 26
All functions in a script must be closed with an 'end'.
Error in sqroot (line 70)
info_plot(tru_err_arr, est_err_arr, iter_arr)
Error in main (line 4)
sqroot(num);
never i have figure out thanks
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics Objects dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by