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 commentaires

Rik
Rik le 2 Sep 2020
Where did you store the NumericalSol and ExactSol functions?
How to store them?
Rik
Rik le 2 Sep 2020
You didn't write the function on a piece of paper. In which file did you store the function? And where is that file located relative to you current folder?
The code Bjorn suggested ( which('ExactSol') ) should answer most of that question.
Muhammad Umar Khan
Muhammad Umar Khan le 2 Sep 2020
Modifié(e) : Rik le 2 Sep 2020
All are in same folder File path is c:desktop/matlab
I can track Assignment_1 but can't track function
Rik
Rik le 2 Sep 2020
So those two functions are in Assignment_1.m?
I would suggest you go back to the previous lessons of your Matlab course to have a look again at the fundamentals. Another option is to do the Onramp course that is provided by Mathworks for free.
Taxact in Assignment_1 and T_NumericalSol in Assignment_2
Rik
Rik le 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.

Connectez-vous pour commenter.

 Réponse acceptée

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 commentaires

I have done all this but it still shows the same error
So what does
which ExactSol
return?
Can't find ExactSol
Bjorn Gustavsson
Bjorn Gustavsson le 2 Sep 2020
OK, do this (with suitable changes to your folder names):
  • save the ExactSol-function in a file ExactSol.m and the function _ NumericalSol_ in a file NumericalSol.m
  • Make sure that you have the directory where you save these functions on the matlab-path, as outlined above.
HTH
I agree with Bjorn. If the name of a function in a function file does not match the name of the function file itself, MATLAB will identify the function using the file name not the function name. [See the first Note on this documentation page.] If the following code was in a file named banana.m
function y = watermelon(x)
y = 2*x;
end
you would call this function as
y = banana(1:10)
not as
y = watermelon(1:10)
Thank you I got
Bjorn Gustavsson
Bjorn Gustavsson le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

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

Community Treasure Hunt

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

Start Hunting!

Translated by