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

  • MATLAB
  • Thread starter rc flyer uk
  • Start date
  • Tags
    Matlab
In summary, the conversation discusses different approaches for calculating t3is in MATLAB, with the use of the 'arrayfun' function and vectorization being suggested as potential solutions. The conversation also touches on the use of anonymous functions and proper coding practices.
  • #1
rc flyer uk
11
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
  • #2
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
 
  • #3
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.
 
  • #4
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).
 

What is Matlab and what is it used for?

Matlab is a high-level programming language and interactive environment commonly used in scientific and engineering applications. It allows for data manipulation, visualization, and numerical computation.

How do I install Matlab?

To install Matlab, you will need to purchase a license or obtain a trial version from the official website. Once downloaded, follow the installation instructions provided by the software.

What are the basic syntax and commands in Matlab?

The basic syntax and commands in Matlab are similar to other programming languages. It includes variables, arrays, conditional statements, loops, and functions. Familiarizing yourself with these concepts will help you write and understand Matlab code.

How do I import and export data in Matlab?

Data can be imported into Matlab from various file formats such as CSV, Excel, and text files. You can also export data from Matlab into these formats. The specific commands and functions for data import and export can be found in the Matlab documentation.

How can I learn more about Matlab?

There are various resources available to learn more about Matlab, such as online tutorials, official documentation, and community forums. You can also enroll in courses or attend workshops to improve your understanding and skills in using Matlab.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Mechanical Engineering
Replies
2
Views
703
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
790
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top