Solving for T3is in Matlab: Tips for Using XCount and Other Functions

  • Context: MATLAB 
  • Thread starter Thread starter rc flyer uk
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around solving for the variable t3is in MATLAB using various functions and approaches. Participants explore methods to handle matrix operations, particularly focusing on the challenges of applying exponentiation to arrays.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant expresses uncertainty about which MATLAB function to use for calculating t3is, specifically mentioning the issue of exponentiating a matrix.
  • Another participant suggests using the 'arrayfun' function to apply a power operation to each element of the array, providing a code example for clarity.
  • A different participant proposes vectorizing the variables and using element-wise operations, noting the importance of using dots in front of operators for element-wise calculations.
  • Another response indicates that the original formula can be simplified directly without additional functions, emphasizing the use of element-wise operations for clarity.

Areas of Agreement / Disagreement

Participants present multiple approaches to the problem, and while there are suggestions for different methods, there is no consensus on which method is superior or preferred.

Contextual Notes

Some participants mention performance considerations regarding the use of 'arrayfun' versus vectorization, but no definitive conclusions are drawn about efficiency or best practices.

Who May Find This Useful

This discussion may be useful for MATLAB users, particularly those dealing with matrix operations and looking for efficient coding techniques in mathematical computations.

rc flyer uk
Messages
11
Reaction score
0
I need some help with matlab! not sure which function to use!

I think i should use xcount ycount so i can do it but i shall explain the problem!

I have input some variables into matlab

p2=1e5
t2=288
p3=[2e5:1e4:10e5]
y=1.4

and now i wish to work out
t3is which should be:

t3is=t2*((p3/p2)^(y-1/y))

but that can't be done because you can't ^ a matrix!

So could i use xcount to do it or what is the best way!

Thanks in advance!

Matlab Noob:confused:
 
Physics news on Phys.org
You can use the function 'arrayfun' to apply a function to each element of an array.

How about code like this?

Code:
p2=1e5
t2=288
p3=[2e5:1e4:10e5]
y=1.4

t3is = p3/p2
arrayfun(@(x) x^(y - 1/y), t3is)
t3is = t3is * t2;

The notation @(x) defines a new anonymous function that acts on x. In this case, all it does is raise x to the (y - 1/y) power.

You can, of course, combine all three of the last lines into one, if you prefer:

Code:
t3is = t2 * arrayfun(@(x) x^(y - 1/y), p3/p2)

- Warren
 
Alternatively you can vectorise all variables Since I have not used arrayfun before I do not know which is faster.

Code:
p3=[2e5:1e4:10e5];
p2=1e5*ones(size(p3));
t2=288*ones(size(p3));
y=1.4*ones(size(p3));
t3is=t2.*((p3./p2).^(y-ones(size(p3))./y));

The dots in front of the operators perform element by element operations between matrices of the same size. You don't need it for + or -. And I'm pretty sure you've found out this by now: putting semi-colons behind each line suppresses the outputs, keeping your command window neat and managable.
 
You can simply do this...

t2*((p3/p2).^(y-1/y))

if you want each element of (p3/p2) raised to the power (y-1/y).
 

Similar threads

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