How Do I Perform Variable Calculations and Write Code in MATLAB?

  • Context: MATLAB 
  • Thread starter Thread starter atlbraves49
  • Start date Start date
  • Tags Tags
    Basics Matlab
Click For Summary

Discussion Overview

The discussion revolves around performing variable calculations and writing code in MATLAB, specifically focusing on defining variables, calculating values for equations, and utilizing MATLAB's features for programming. It includes questions about basic coding practices and seeks guidance on tutorials and resources.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant asks if a blank new M-file should be created for writing MATLAB code.
  • Another participant expresses uncertainty about how to define variables and calculate values for an equation as they vary two variables.
  • A specific equation is presented: y = x^(-0.5) + z^(ln(x)), with a request for guidance on how to input different values for x and z.
  • Several participants suggest that there are many online resources and tutorials available for learning MATLAB basics.
  • One participant mentions the usefulness of MATLAB's Help-files for learning.
  • A suggestion is made to use inline functions and vector operations for calculations, providing an example of calculating sin(x) over a specified range.
  • Links to external resources, including Wikibooks and MathWorks documentation, are provided for further learning.

Areas of Agreement / Disagreement

Participants generally agree that there are many resources available for learning MATLAB, but the original poster expresses difficulty in finding specific information regarding equations and variables. The discussion remains unresolved regarding the specific coding questions posed.

Contextual Notes

Some participants reference the use of inline functions and vector operations, but there is no consensus on the best approach to the original poster's specific questions about defining variables and performing calculations.

Who May Find This Useful

Individuals new to MATLAB programming, particularly those looking for guidance on variable calculations and coding practices.

atlbraves49
Messages
80
Reaction score
0
I need some help with some basic MATLAB questions.

1) When writing a MATLAB code, do I create a blank new M-file, and write it there?

2) I need the program to calculate values for an equation, as I vary two variable within the equation. I'm unsure how to do this (for example, how do I define the variables, etc)

For example, say the equation is y = x^(-0.5) +z^(ln(x));

I want the program to give me values for y when i input different values for x and z. How is this accomplished?

If anyone can point me to good MATLAB tutorials that go into this kind of detail, I'd appreciate it. I've looked, including MATLABs help, and haven't found anything regarding equations and variables, etc.

Lastly, how do you comment in MATLAB?
 
Physics news on Phys.org
There are hundreds of web pages out there that give the basics for how to do these things in Matlab. A quick Google search will get you going.
 
And the Help-files that come with Matlab are actually very good...
 
atlbraves49 said:
I've looked, including MATLABs help, and haven't found anything regarding equations and variables, etc.



Can anyone provide some actual help regarding these questions?
 
atlbraves49 said:
Can anyone provide some actual help regarding these questions?

Regarding this specific question, you're probably looking for an inline function:
http://en.wikibooks.org/wiki/MATLAB_Programming/Portable_Functions

That's the serial way of doing things. However, you can often perform operations on entire vectors. If you wanted to calculate, say, sin(x) between 0 and 2pi, in increments of 1/200*pi (and then plot the result), you'd do the following:

>> x=0:1/200*pi:2*pi %The colon format goes start:increment:end
>> y=sin(x) %This will generate a vector of y values for a bunch of x's
>> plot(x,y)

Additionally, perhaps start here:
http://en.wikibooks.org/wiki/MATLAB_Programming

Or here:
http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/bqr_2pl.html

But if you need a quick reference for a command within MATLAB, just type help COMMAND, for instance, getting help about the identity matrix creation function:
>> help eye
 

Similar threads

Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K