Azzera filtri
Azzera filtri

Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

Help with the use of function

3 visualizzazioni (ultimi 30 giorni)
Pragya Srivastava
Pragya Srivastava il 22 Feb 2017
Chiuso: MATLAB Answer Bot il 20 Ago 2021
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
Pragya Srivastava
Pragya Srivastava il 22 Feb 2017
dx is a scalar. Also if I execute the same command in the command prompt and not within the function then there is no error message. Which is why I concluded that the issue is in calling the function and not in the syntax. Same syntax works perfectly on command-line.
Jan
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
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.

Guillaume
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
Pragya Srivastava
Pragya Srivastava il 22 Feb 2017
Thank you. Just as a reply to the points raised
1. about supposedly more number of input variables than are used, the function actually has more syntax which makes use of those. I had commented that section and yet was getting error, hence I did not paste that part.
2. dx is spatial grid, dt is time step hence they are not same.
3. Also the drive for defining global variables is to be able to set their values just in the main script instead of changing them in each function, every time. The global variables are actually the physical parameters of the system and their use is motivated by a practical point in mind(not laziness). Thank you for answers. I will use debugger.

Questa domanda è chiusa.

Community Treasure Hunt

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

Start Hunting!

Translated by