[matlab] something wrong with my code

  • MATLAB
  • Thread starter Ann_Hope
  • Start date
  • Tags
    Code Matlab
In summary, the conversation is about a person new to using MATLAB/Scilab who wants to create a 3D surface using a specific equation. They encountered an error message when trying to input the equation and asked for help. Another person suggested using the "mesh" function and provided an example from the MATLAB website with some comments explaining the code.
  • #1
Ann_Hope
2
0
Hi all
I'm new to matlab/scilab. I want to draw a 3D surface using z = cos(x+y)*cos(3x-y)+cos(x-y)*sin(x+3y)+5e^(x2+y2)/8

I typed "Z=cos(x+y)*cos(3*x-y)+cos(x-y)*sin(x+3*y)+5*exp(-(x.^2+y.^2)/8);" into matlab, but is says "Inconsistent multiplication".

I would appreciate anyone who could help me check the problem and give me an advice.

Thanks all!
 
Physics news on Phys.org
  • #2
a) how have you defined X and Y
b) I'd use mesh:

example said:
figure
[X,Y] = meshgrid(-8:.5:8); %<------------HERE they define X and Y
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
mesh(X,Y,Z) %<-----------THIS is the MATLAB function that turns all those values into a visual plot
example from http://www.mathworks.com/help/techdoc/ref/mesh.html with comments by me
 

Related to [matlab] something wrong with my code

1. Why am I getting an error when running my code?

There could be a few reasons for this. First, check for any typos or syntax errors in your code. If there are none, then the error could be due to a mistake in your logic or a missing variable declaration. It's also possible that there is an issue with your installation of MATLAB or with the version you are using. Try updating or reinstalling MATLAB to see if that solves the problem.

2. How can I debug my code?

MATLAB has a built-in debugger that can help you identify and resolve errors in your code. You can set breakpoints to pause the code at specific points and inspect the values of variables. You can also use the "dbstop" function to stop the code when certain conditions are met. Additionally, you can use the "disp" function to print out the values of variables at different points in your code.

3. Why is my code running slowly?

There are a few potential reasons for this. One possibility is that your code is computationally intensive and is taking a long time to run. In this case, you may need to optimize your code or consider using a different algorithm. Another reason could be that you are using a lot of nested loops, which can significantly slow down your code. Try to avoid using nested loops and instead utilize vectorization techniques. Finally, it's also possible that your computer's resources are being used by other programs or processes, causing your code to run slower.

4. How can I improve the performance of my code?

One way to improve the performance of your code is to use vectorization techniques instead of nested loops. This means using MATLAB's built-in functions that operate on arrays, rather than looping through each element individually. You can also preallocate memory for arrays before running your code, which can speed up the execution. Another option is to use parallel processing to split the workload among multiple cores or processors, if your computer has them.

5. What should I do if I can't find the source of the error in my code?

If you have thoroughly checked your code and still can't identify the source of the error, it may be helpful to reach out to the MATLAB community for assistance. You can post your code and the error message on MATLAB forums or ask for help on social media platforms like Twitter or Reddit. Additionally, you can contact MathWorks support for further assistance with troubleshooting your code.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
677
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
221
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
646
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top