Solving MATLAB Exponent Problem: Tips & Fixes

  • Context: MATLAB 
  • Thread starter Thread starter swartzism
  • Start date Start date
  • Tags Tags
    Exponent Matlab
Click For Summary
SUMMARY

The discussion addresses a common issue encountered in MATLAB when attempting to compute the shoreline depth using the equation h=A*y^(2/3). The error arises from the misuse of the power operator, which requires element-wise operations for vector inputs. The correct implementation is h=A*y.^(2/3), which allows for proper calculation across the vector y. This solution effectively resolves the error and produces the desired graph of h versus y.

PREREQUISITES
  • Basic understanding of MATLAB syntax and operations
  • Familiarity with vectorized operations in MATLAB
  • Knowledge of plotting functions in MATLAB
  • Understanding of mathematical concepts related to power functions
NEXT STEPS
  • Explore MATLAB's element-wise operations, specifically the dot operator.
  • Learn about MATLAB's plotting functions to enhance data visualization.
  • Review MATLAB documentation on the power function and its applications.
  • Investigate common MATLAB errors and debugging techniques.
USEFUL FOR

Students, researchers, and engineers working with MATLAB who need to perform mathematical computations and visualizations effectively.

swartzism
Messages
103
Reaction score
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
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
 

Similar threads

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