How can I fix matrix and integration errors in my MATLAB code?

In summary, the author is trying to integrate a function func1 in Matlab, but is getting error messages.
  • #1
krnhseya
103
0
Hello, I've got quick questions.
1. When I have a matrix, x, and try to get different values for each x (given y = (x^2)+b),
I get this error.

? Error using ==> mpower
Matrix must be square.

2. x is [1x101] double according to MATLAB and I've had this problem when I was taking derivative as well. I've lost numbers throughout the derivative, which i understands.
so x' = [1x100] and x'' = [1x99] and so forth.

What's the best way to solve these problems.
Thank you.
 
Physics news on Phys.org
  • #2
krnhseya said:
Hello, I've got quick questions.
1. When I have a matrix, x, and try to get different values for each x (given y = (x^2)+b),
I get this error.

? Error using ==> mpower
Matrix must be square.

2. x is [1x101] double according to MATLAB and I've had this problem when I was taking derivative as well. I've lost numbers throughout the derivative, which i understands.
so x' = [1x100] and x'' = [1x99] and so forth.

What's the best way to solve these problems.
Thank you.

Can you post your code? I don't really understand your question.
 
  • #3
>> x=sqrt(500/105);
>> a=[0:0.1:100];
>> b=[0:1:100];
>> z=a/(2*(sqrt(3000*15)));
>> p=x(sqrt(1-((z)^2)))
? Error using ==> mpower
Matrix must be square.

this is not the direct problem but it's basically the same thing.
Why am I getting this error?

I will hold onto my second question for now.

[edit] another question...

>> x=A*(exp(-(z*wn*t)))*(sin(wd*t))
? Error using ==> mtimes
Inner matrix dimensions must agree.

everything is numeric except t is in matrix...
 
Last edited:
  • #4
krnhseya said:
>> x=sqrt(500/105);
>> a=[0:0.1:100];
>> b=[0:1:100];
>> z=a/(2*(sqrt(3000*15)));
>> p=x(sqrt(1-((z)^2)))
? Error using ==> mpower
Matrix must be square.

this is not the direct problem but it's basically the same thing.
Why am I getting this error?

It looks like you are using the matrix power operator ^ where you should be using the array power operator .^ . The matrix power operator A^n multiplies the matrix A by itself n times, which means the number of columns in A must be equal to the number of rows in A, which means that A must be square (size(A) = [m x m] for some positive integer m). The array power operator A.^n raises each element of A to the nth power, and therefore has no restrictions on the dimensions of A.

another question...

>> x=A*(exp(-(z*wn*t)))*(sin(wd*t))
? Error using ==> mtimes
Inner matrix dimensions must agree.

everything is numeric except t is in matrix...

Once again, it looks like you are using a matrix operator where you should be using an array operator. Try (exp(-z*wn*t))) .* (sin(wd*t))
 
  • #5
thanks a lot! I've got another question...sorry.

i am given that function and there are two independent variables, t and a, which i posted above. i am told that it's initial displacement is zero but it "dies out" after about t=2.
so basically, after x has been graphed up to t<2 and when t hits 2 seconds mark, i think everything converges to zero i believe.

i tried everything without using mfile but i couldn't figure out so i thought i should use mfile instead so I've retyped everything under...



t=[0:0.1:10];
b=[0:1:100];

if 0=<t<2 (BTW, if i want less than equal to 0, i should use =< instead of <= right?)
all the components that vary by a and b goes under if statement including equation of x
else
x=0
end
plot(t,x)


that's what I've tried but it doesn't seem like i am getting what i wanted. it graphes entire 10s and under mfile, it states something like i can't use this, "0=<t<2", because it's in matrix form but i can't think of any other way to approact this problem.
i've thought about while loop and etc. but it seems like that my if,while,and other statements are not working because of how t is in matrix.

i really appreciate your help.
 
  • #6
Help with integrating function in Matlab

Hi I'm trying to integrate a function func1 in Matlab but I keep getting errors I really don't know what I'm doing wrong. Can anyone tell me?

Here is the code I am using:

function [ f1 ] = func1( E, Delta, kB, Temp )
%func1: calculates integrand for eqn 1
% Detailed explanation goes here
f1=(tanh(sqrt(((E*E)+(Delta*Delta))))/(2*kB*Temp))/(2*(sqrt(((E*E)+(Delta*Delta)))));
end

Command line code:

>> hbar=1.054E-34; Tdebye=420; h=6.626E-34; kB1= 1.38065E-23;
>>fdebye=(Tdebye*kB1)/h;
>> Temp1=0.5;
>> Delta1=0;
>> a=hbar*fdebye;
>> Q=quad(@(E1)func1(E1,0,1.38065E-23,0.5),-a,a)

? Error using ==> mtimes
Inner matrix dimensions must agree.

Error in ==> func1 at 4
f1=(tanh(sqrt(((E*E)+(Delta*Delta))))/(2*kB*Temp))/(2*(sqrt(((E*E)+(Delta*Delta)))));

Error in ==> @(E1)func1(E1,0,1.38065E-23,0.5)


Error in ==> quad at 77
y = f(x, varargin{:});
 

Related to How can I fix matrix and integration errors in my MATLAB code?

1. What is a square matrix in MATLAB?

A square matrix in MATLAB is a matrix with an equal number of rows and columns. For example, a 3x3 matrix or a 5x5 matrix are both considered square matrices.

2. How do I create a square matrix in MATLAB?

To create a square matrix in MATLAB, you can use the "eye" function which creates an identity matrix with equal number of rows and columns. You can also use the "zeros" or "ones" function and specify the same number for both rows and columns. Alternatively, you can manually input the values for your matrix in the command window.

3. How do I calculate the determinant of a square matrix in MATLAB?

To calculate the determinant of a square matrix in MATLAB, you can use the "det" function. This will return a single value representing the determinant of the matrix.

4. Can I perform matrix multiplication on two square matrices in MATLAB?

Yes, you can perform matrix multiplication on two square matrices in MATLAB using the "*" operator. It is important to ensure that the number of columns of the first matrix is equal to the number of rows of the second matrix.

5. How do I find the inverse of a square matrix in MATLAB?

To find the inverse of a square matrix in MATLAB, you can use the "inv" function. This will return the inverse of the matrix as a new matrix. It is important to note that not all square matrices have an inverse, so you may need to check for singularity before using the "inv" function.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
203
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
921
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
968
Back
Top