Info
Questa domanda è chiusa. Riaprila per modificarla o per rispondere.
Help with the use of function
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi, I am trying to define my own function with really simple syntax as following
function [rhs]=rk4_RDM(c_old,kon,dim,dt)
global DL koff dx
rhs=(circshift(c_old,-1,dim)+circshift(c_old,1,dim)-2*c_old)/(dx^2);
end
Calling this function always results in an error of matrix dimension mismatch. If I execute the same command
rhs=(circshift(c_old,-1,dim)+circshift(c_old,1,dim)-2*c_old)/(dx^2);
without calling the function, there is no such error. So apparently there is some issue in calling the function. The way I am doing it is simply
y=rk4_RDM(c_old,kon,dim,dt)
Could you please let me know if you find a mistake in this way of calling a function or what I might be doing wrong ?
Thanks
5 Commenti
Jan
il 22 Feb 2017
Then you have other inputs in the command window. Please post the inputs and please please please format you code to improve the readability. We do not have the inputs, the function, a complete copy of the error message and have problems to read the code. Then it is really hard to provide assistence. The less we guess the better and easier is the answer.
Risposte (2)
Jan
il 22 Feb 2017
You can use the debugger to see, what's going on:
dbstop if error
Then start the program again until Matlab stops at the error. Now you can examine the dimensions of the involved variables in the command window or in the Workspace browser.
0 Commenti
Guillaume
il 22 Feb 2017
As per Jan's advice, the debugger is the best tool to use to find what is going wrong.
"if I execute the same command in the command prompt and not within the function then there is no error message"
Since the workspace of the function is different from your main workspace, this indicates nothing. There are two things with your function that seem to indicate it's not doing what it should:
- Use of global variables. As Jan also said, global variables are a bad idea. Usually an indication or poor design (or laziness). In this particular functions two of the global are not even used and there's no reason why the 3rd one (dx) cannot be passed as a normal argument.
- Two of the input variables, kon and dt are not used by the function. Therefore, why are they an input? Is the dx in the formula meant to be dt instead?
But, again, as Jan said, use the debugger. When it breaks because of the error you can immediately see what the content of dx, c_old and dim actually are as opposed to what you think they are.
1 Commento
Questa domanda è chiusa.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!