Solving MATLAB Exponent Problem: Tips & Fixes

In summary, the conversation is about a person trying to solve a problem on MATLAB but facing challenges due to lack of knowledge. They share their code and explain their desired output, but are getting an error related to the use of the mpower function. The expert suggests using the power operator instead and provides documentation for further understanding.
  • #1
swartzism
103
0
I am trying to do a very simple problem on MATLAB, but I do not know much about MATLAB so this is challenging. Here is the code:

Code:
y=0:50:300;
D=0.210;          % D is the mean sediment diameter
w=14*D^(1.1);     % w is the particle fall speed
A=0.067*w^(0.44); % A is the sediment parameter
h=A*y^(2/3);      % h is the shoreline depth under water
plot (y,h);

What I would like to happen is produce a graph of h=1.005*y^(2/3) with coordinates of h from 0 to 5 in increments of 0.5 and coordinates of y from 0 to 300 in increments of 50. The error I get is:

? Error using ==> mpower
Inputs must be a scalar and a square matrix.

Error in ==> beachprof at 5
h=A*y^(2/3); % h is the shoreline depth under water


What is going on here and how can I fix it?
 
Physics news on Phys.org
  • #2
Your problem is in this line:

Code:
h=A*y^(2/3);

What you want is:

Code:
h=A*y.^(2/3);

To understand the difference, read the docs:

Code:
doc mpower
doc power
 

1. How do I write an exponent in MATLAB?

To write an exponent in MATLAB, use the caret (^) symbol. For example, to write 2 to the power of 3, you would type 2^3.

2. Why am I getting an error when trying to solve an exponent problem in MATLAB?

There could be several reasons for this error. One common mistake is forgetting to use the caret symbol (^) when writing an exponent. Another possibility is using incorrect syntax, such as using an asterisk (*) instead of the caret. Make sure to carefully check your code for any syntax errors.

3. How can I solve exponential equations in MATLAB?

To solve exponential equations in MATLAB, you can use the built-in function exp. For example, to solve the equation e^x = 5, you would use the code x = log(5).

4. Can I use fractions as exponents in MATLAB?

Yes, you can use fractions as exponents in MATLAB. Simply write the fraction as a decimal or use the power function. For example, to write 2^(1/2), you can either type 2^0.5 or use the code power(2, 1/2).

5. How can I display the result of an exponent problem in MATLAB with a specific number of decimal places?

To display the result of an exponent problem with a specific number of decimal places, you can use the fprintf function. For example, if you want to display the result of 2^3 with 2 decimal places, you would use the code fprintf('%.2f', 2^3).

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • Introductory Physics Homework Help
Replies
1
Views
985
  • Engineering and Comp Sci Homework Help
Replies
2
Views
813
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
866
Back
Top