Whats wrong with this matlab code?

In summary: I copied-and-pasted the code on the Matlab (R11.1) command prompt and a figure appears. Nothing appears in the command line though.
  • #1
ranger
Gold Member
1,687
2
I have a very simply MATLAB code. When I try go run it, I get no graph and switching over to the MATLAB command line, I see:
ans =

char

Here is the code:
Code:
t = linspace(0,60);
v = 3.22;

for q_0 = [0:1:2007];
    q = q_0*exp(v*t);
    plot(t,q)
    hold on
end
It looks just fine to me.

Any feedback would be great.
 
Physics news on Phys.org
  • #2
It runs fine in MATLAB R2006b on my computer.

Perhaps try removing the semicolon at the end of "for q_0 = [0:1:2007];"

- Warren
 
  • #3
I still get no luck. I can run other m-files from the same directory with no problems. Does it say anything on the MATLAB command line when you run it?
 
  • #4
I have copied-and-pasted the script on the Matlab (R11.1) command prompt and a figure appears. Nothing appears in the command line though.

How did you try to run the script?
 
  • #5
It looks to me to be plotting every point in a separate graph,, try writing to an array and plot after the loop is complete. I;ll try at work tommorow where I have Matlab R2006B.
 
  • #6
doodle said:
I have copied-and-pasted the script on the Matlab (R11.1) command prompt and a figure appears. Nothing appears in the command line though.

How did you try to run the script?

I tried to run it from the m-file editor screen. The usual Debug>Run. I also even tried to get it from the command line, but I still get ans = char as an output.

Dr Transport said:
It looks to me to be plotting every point in a separate graph,, try writing to an array and plot after the loop is complete. I;ll try at work tommorow where I have Matlab R2006B.
I don't think its separate graphs. Its done on one graph but the "hold on" command allows me to plot different values for q_0 on the same graph.


Its is possible for anyone to post a screenshot of the graph? btw, I'm using R14.
 
  • #7
ranger said:
Its is possible for anyone to post a screenshot of the graph? btw, I'm using R14.

Here's a screenshot...
 

Attachments

  • 01.jpg
    01.jpg
    34 KB · Views: 455
  • #8
Thanks doodle. It seems I have to do a little tweaking of my domain, but its what I expected it to be.
 
  • #9
It turns out I was getting this error becuase the name of the M-file had a white space. Beats me...
 

What is the purpose of this Matlab code?

The purpose of this Matlab code is to perform a specific task or calculation, which is defined by the programmer.

Why is the code not producing the desired output?

There could be several reasons for this, such as errors in the code, incorrect input values, or a mistake in the logic of the code. It is important to carefully review the code and check for any errors or issues that could be causing the incorrect output.

How can I debug this Matlab code?

One way to debug Matlab code is to use the debugging tools provided by Matlab, such as the "debug" function or the "keyboard" command. These tools allow you to pause the code at a specific point and inspect the values of variables to identify any errors or issues.

Can this code be optimized for better performance?

Yes, Matlab code can be optimized for better performance by using vectorization, preallocation of variables, and avoiding unnecessary loops or function calls. It is important to identify the bottleneck in the code and optimize it accordingly.

What are some common mistakes to avoid when writing Matlab code?

Some common mistakes to avoid when writing Matlab code include not using proper syntax, not preallocating variables, not properly handling errors, and not following good programming practices such as breaking the code into smaller, modular functions.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Back
Top