Can't call my function to another m file
23 views (last 30 days)
Show older comments
Muhammad Umar Khan
on 2 Sep 2020
Commented: Bjorn Gustavsson
on 2 Sep 2020
I have two m files containing in one folder. I want to calculate error of exact and numerical solution on the third m file. The functions names are
T_exact=ExactSol(x,t,alpha,y);
T_num=NumericalSol(x,nj,r,t_end);
The code to the third file is given below:
alpha=10^-3;
T1=800;
T2=200;
nj=100;
T0=T1*ones(nj,1);
T0(nj/2+1:nj)=T2*ones(1,nj/2);
r=0.4;
t_end=50;
delta_x=1/nj;
x=([1+nj]-0.5)/nj;
T_num=NumericalSol(x,nj,r,t_end);
T_exact=ExactSol(x,t,alpha,y)
err=sqrt(delta_x*(T_num-T_exact)*(T_num-T_exact))
The Names of two m files are Assigment_2 and Assigment_1. I am recieving an error that T_num and T_exact are't defined. How to solve this problem?
7 Comments
Rik
on 2 Sep 2020
Please attach these two files to your question. You're treating them as functions in the code you posted, but it is unclear if you made them actual functions.
Accepted Answer
Bjorn Gustavsson
on 2 Sep 2020
If you have your third m-file in another directory and the first directory is not on the matlab-path then matlab will not know about their existence. You can check this with
which ExactSol
If that doesn't return with the full path to your function then you have to add that first directory to you path. I typically do that something like this:
addpath /home/bjorn/matlab/first_directory_name -end
where you can make the suitable replacements. I use -end just to make sure that I dont by accident shadow any of matlab's functions with any of my functions - this is unusual but then will break some matlab-functionality.
Also check the documentation for path.
HTH
7 Comments
Bjorn Gustavsson
on 2 Sep 2020
@Steven, and everyone - even if it worked to call functions "by-name-hidden-in-files-with-other-names" it would be a nightmare trying to remember what files to edit and maintain.
More Answers (0)
See Also
Categories
Find more on Search Path in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!