Small matlab code not working. Why ?

In summary, MATLAB thinks that the function "mubeton" is a function, but it's just a vector of double values. Breaking the huge line defining eps into several "baby-step" intermediate calculations on separate lines solved the problem.
  • #1
imsolost
18
1
Code is the following :
Matlab:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xiter=[0.7 20 10];
mubeton=[0.100 0.80 0.70 0.60 0.50 0.25];
muair=[0.05 0.042 0.04 0.032 0.021 0.011];
zl=[5 30 70 100];

fnorm1 = @(d,a0,dmax) (a0*(1-exp(-(0.5*log(d./dmax)).^2))+exp(-(0.5*log(d./dmax)).^2));
fnorm2 = @(d,dmax,DRL) exp(-(d-dmax)./DRL);
integ=integral(@(r)((r<xiter(2)).*fnorm1(r,xiter(1),xiter(2))+(r>=xiter(2)).*fnorm2(r,xiter(2),xiter(3))),0,inf); %xiter manquant en input
funny=@(r,xiter)((r<xiter(2)).*fnorm1(r,xiter(1),xiter(2))+(r>=xiter(2)).*fnorm2(r,xiter(2),xiter(3)))./integ;

eps=@(r)(integral2(@(R,phi)(exp(-mubeton(1).*(sqrt((zl(1)+r).*(zl(1)+r) + R.*R) - zl(1).*sqrt((zl(1)+r).*(zl(1)+r) + R.*R)./(zl(1)+r))-muair(1).*(zl(1).*sqrt((zl(1)+r).*(zl(1)+r) + R.*R)./(zl(1)+r))).*R./(2.*sqrt(R.*R+(zl(1)+r).*(zl(z1)+r)))),0,inf,0,2*pi));
%produit=@(y)((eps(y)).*((funny(y,[0.7 20 10]))));
integral(@(y)((eps(y)).*((funny(y,[0.7 20 10])))),0,inf,'ArrayValued',true)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Error is the following :

"Undefined function 'mubeton' for input arguments of type 'double'."

Why the hell MATLAB thinks this is a function wile its clearly expressed as a vector ?

Thx for any help guys :)
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Perhaps because you have "mubeton(1)"?
 
  • #3
Thank you for your answer but i don't get what you mean. I want indeed to use the first value stored in the vector mubeton. So mubeton(1) seems right to me.
 
  • #4
Sorry, I made a rushed suggestion on my way out the door without looking at your code properly so please disregard my answer.
 
  • #5
The line where mubeton(1) is used is awfully long. Have you tried to break that up into multiple lines of smaller intermediate results? Thinking an interpreter weirdness. [EDIT: not likely. Have you tried a clear all?] Also this is not the whole code, so I can't reproduce your error message.
 
Last edited:
  • Like
Likes FactChecker
  • #6
I suggest breaking that huge line defining eps into several "baby-step" intermediate calculations on separate lines. Error messages are no always exact about the error location.
Before that bunch of smaller lines, set temp1 = mubeton(1) and see if it accepts it.

PS. I don't have MATLAB so I can't give specific help.
 
  • #7
lewando said:
The line where mubeton(1) is used is awfully long. Have you tried to break that up into multiple lines of smaller intermediate results? Thinking an interpreter weirdness. [EDIT: not likely. Have you tried a clear all?] Also this is not the whole code, so I can't reproduce your error message.

I followed your suggestion and rewrote the code with more intermediate calculation subfunctions. It worked since problem seems to be gone.

Thank you all guys for this help.
 
  • Like
Likes FactChecker

Related to Small matlab code not working. Why ?

1. Why is my small matlab code not producing any output?

There could be several reasons for this. First, make sure that your code is written correctly and there are no syntax errors. Second, check your inputs and make sure they are in the correct format. Third, check if your code is stuck in an infinite loop. Finally, make sure that your code is actually being executed and not just saved without running.

2. Why am I getting an error message when I run my small matlab code?

Errors are a common occurrence in coding, and they can happen for various reasons. One possible reason is that there is a mistake in your code, such as a typo or a missing function. Another reason could be that your inputs are not compatible with the functions you are using. It is also possible that your code is trying to access a file or variable that does not exist. Check the error message to identify the specific issue and try to troubleshoot from there.

3. How can I debug my small matlab code?

Debugging is an essential part of coding, and there are several tools and techniques you can use to identify and fix errors in your code. You can use the built-in debugger in matlab, which allows you to step through your code and see the values of variables at each step. You can also use print statements to check the values of variables at different points in your code. Additionally, you can use breakpoints to stop the execution of your code at a specific line and examine the state of your program.

4. Why is my small matlab code running slowly?

There are various factors that can affect the speed of your code. One possibility is that your code is written inefficiently, and there are better ways to accomplish the same task. Another reason could be that you are using large datasets or complex algorithms, which naturally take longer to run. You can optimize your code by using vectorization, preallocating variables, and avoiding unnecessary loops. You can also try running your code on a faster computer or using parallel processing techniques.

5. How can I improve the performance of my small matlab code?

To improve the performance of your code, you can follow several best practices. First, avoid unnecessary loops and use vectorized operations whenever possible. Second, preallocate variables to avoid constantly resizing them during execution. Third, use built-in functions and avoid re-inventing the wheel. Fourth, use parallel processing techniques to distribute the workload among multiple cores. Finally, monitor the memory usage of your code and try to optimize it if it is taking up too much memory.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
495
Back
Top