Issue with output of an indirect equation using Fzero - matlab

I am stuck on trying to write the output of a for loop into a vector. The issue is that once it goes through the equation, it stops and spits out this error: "Subscript indices must either be real positive
integers or logicals."
I've looked into what this error is and I've checked my code. There are no negative integers being output, nor any zeros. The for-loop, function and the fzero command are all good, if I comment out MW4p(y4,:) = MW4 I get all of my answers on the command window. I just can't put them into an array.
for y4 = linspace(1.4,1.67,100)
x0 = 12; %Starting Point
fun = #(MW4) (y4.*MW1.*T3)./(y1.*MW4.*T2) - ...
(((1+((y4+1)./(y4-1)).*p5p2).*((2.*y4)./(y4-1))) ./ ((1+...
((y1+1)./(y1-1)).*p5p2).*((2.*y1)./(y1-1)))) .* (((2./(y1-1))...
./ (2./(y4-1))).^2);
MW4 = fzero(fun,x0)
MW4p(y4,:) = MW4
end
where
y1 = 1.67;
MW1 = 39.55;
T3 = 250; %k
T2 = 700; %k
p5p2 = 2.307; %Determined from T5 desired
MW4p = ones(1,100);
In earlier attempts, I've tried MW4p(y4) = MW4 which had this error: "Attempted to access MW4p(1.4); index must be a
positive integer or logical." So I added a colon. Putting this command outside of the for-loop simply had it access the last value of the loop and return the same error.

The error message is telling you exactly what you need to know. y4 is not an integer. When using vectors, matrices, cells, or arrays you can't index with a non-integer number.
vectors, matrices and arrays are discrete structures that can only be indexed with discrete values, though obviously they can be used to store non-discrete values.
Unless I'm missing something, your choices are:
Through whatever data structure: store y4 and its corresponding MW4 value as a tuple; for example, you might have an array of structs or cells where one element is y4 and another element is the array returned from MW4
Use something like this answer, using a hash map. I haven't looked into this closely, but my guess is you can use a float as the key.

Related

MATLAB - integral function vectorization warning

When trying to use the integral2 function, I get the following warning:
"Integrand function outputs did not match to the required tolerance when the same input values were supplied in two separate calls with different size input matrices. Check that the function is vectorized properly."
My code is:
z1=0; z2=5;
t1=0; t2=10;
a=1:500;
b=linspace(0.15,0.02,length(a));
c=[a;b];
d=4.9e-07;
func = #(z,t) my_func((z + t),c) .* exp(-d.*t);
int_x = integral2(func,z1,z2,t1,t2,'RelTol',1e-3,'AbsTol',1e-3) ./ (z2 - z1);
my_func is:
function out = my_func(z_in,c)
z=reshape(z_in,1,numel(z_in));
for i=1:length(z)
if (z(i)<0.0)
z(i)=0.0;
end
end
expfactor=exp(-z/150);
sB=5.5*0.9*4*expfactor;
mB=0.9*interpolate(c(1,:),c(2,:),z);
out=sB+mB;
out=reshape(out,size(z_in,1),size(z_in,2));
end
z1=0, z2=9.6, t1=0, t2=3000 (i.e. all have the size (1,1)), and I have used element-wise operations, so not sure what the problem is.
When debugging, my_func is called three times, with z and t having different sizes each time (although same as each other; (14,14), (2,3) and (14,14)). When sizes are (14,14), the values of z are unique for each column, but the same for each row (e.g. [3,2,1;3,2,1]), and vice versa for t (e.g. [4,4,4;5,5,5]). But when sizes are (2,3), all values in the z and t matrices are unique. The warning comes after the second and third call.
What could be the reasons for the warning?
Edit: to accommodate the dimension requirements in my_func, I've reshaped z to a vector at the start, then reshaped the output back to matrix at the end, copying a similar function I've seen. But this returns the warning:
"Non-finite result. The integration was unsuccessful. Singularity likely."
Thanks

How can I access my vectors indices when I am assigning values?

I am trying to code something in Matlab and it involves a lot of accessing elements in vectors. Below is a snippet of code that I am working on:
x(1)=1;
for i=2:18
x(i)=0;
end
for i=1:18
y(i)=1;
end
for i = 0:262124
x(i+18+1) = x(i+7+1) + mod(x(i+1),2);
y(i+18+1) = y(i+10+1) + y(i+7+1) + y(i+5+1) + mod(y(i+1), 2);
end
% n can be = 0, 1, 2,..., 262142
n = 2;
for i = 0: 262142
z(i+1) = x(mod(i+n+1, 262143)); %error: Subscript indices must either be real positive integers or logicals.
end
In the last "for" loop where I am initialising vector z(), I get an error saying: "Subscript indices must either be real positive integers or logicals." However, when I do not suppres z(i+1) by ommiting the semi colon, the program is able to run, and I can see the values of z in the workspace. Why is this?
The code I am writing in Matlab is based upon the series of instructions shown in the image below. However, I can't seem to track down my error which leads to me not being able to access the elements of x() (without not suppressing the output of z()).
I appreciate any ideas :-) Thank you!
The code breaks at that loop last iteration because , for i=262140 you get
(mod(i+n+1, 262143)) = 0
so you cant access x(0) in matlab. the first elements of any variable is x(1).
In addition, and not related to your question, this code doesn't use the advantages matlab has, instead of
for i=2:18
x(i)=0;
end
you can just write:
x(2:18)=0;
etc

Error using sin. Not enough input arguments. Why is that so?

i am solving a mathematical problem and i can't continue do to the error.
I tried all constant with sin^2(x) yet its the same.
clear
clc
t = 1:0.5:10;
theta = linspace(0,pi,19);
x = 2*sin(theta)
y = sin^2*(theta)*(t/4)
Error using sin
Not enough input arguments.
Error in lab2t114 (line 9)
y = sin^2*(theta)*(t/4)
sin is a function so it should be called as sin(value) which in this case is sin(theta) It may help to consider writing everything in intermediate steps:
temp = sin(theta);
y = temp.^2 ...
Once this is done you can always insert lines from previous calculations into the next line, inserting parentheses to ensure order of operations doesn't mess things up. Note in this case you don't really need the parentheses.
y = (sin(theta)).^2;
Finally, Matlab has matrix wise and element wise operations. The element wise operations start with a period '.' In Matlab you can look at, for example, help .* (element wise multiplication) and help * matrix wise calculation. For a scalar, like 2 in your example, this distinction doesn't matter. However for calculating y you need element-wise operations since theta and t are vectors (and in this case you are not looking to do matrix multiplication - I think ...)
t = 1:0.5:10;
theta = linspace(0,pi,19);
x = 2*sin(theta) %times scalar so no .* needed
sin_theta = sin(theta);
sin_theta_squared = sin_theta.^2; %element wise squaring needed since sin_theta is a vector
t_4 = t/4; %divide by scalar, this doesn't need a period
y = sin_theta_squared.*t_4; %element wise multiplication since both variables are arrays
OR
y = sin(theta).^2.*(t/4);
Also note these intermediate variables are largely for learning purposes. It is best not to write actual code like this since, in this case, the last line is a lot cleaner.
EDIT: Brief note, if you fix the sin(theta) error but not the .^ or .* errors, you would get some error like "Error using * Inner matrix dimensions must agree." - this is generally an indication that you forgot to use the element-wise operators

Subscript indices must either be real positive integers or logicals error within Matlab decay program

I am having issues with a code of mine dealing with decay. The error "Subscript indices must either be real positive integers or logicals" continues to occur no matter how many times I attempt to fix the line of code: M=M(t)+h.*F
Here is the complete code so that it may be easier to solve the issue:
M=10000;
M=#(t) M*exp(-4.5*t);
F=-4.5*M(t);
h=.1;
t(1)=0;
tmax=20;
n=(tmax-t(1))/h;
i=1;
while h<=.5
while i<=n
t=t+h;
M=M(t)+h.*F;
data_out=[t,M];
dlmwrite('single_decay_euler_h.txt',data_out,'delimiter','\t','-append');
i=i+1;
end
h=h+.1;
end
Thanks for any help.
In the start, you're setting M = 5000;. In the following line, you're creating an anonymous function also called M:
M=#(t) M*exp(-4.5*t);
Now, your initial M = 5000 variable has been overwritten, and is substituted by the function:
M(t) = 5000 * exp(-4.5*t); %// Note that the first M is used to get 5000
Thereafter you do F = -4.5*M(t). I don't know what the value t is here, but you're giving F the value -4.5 * 5000 * exp(-4.5*t), for some value of t. You are not creating a function F.
In the first iteration of the loop, M=M(t)+h.*F; is interpreted as:
M = 5000 * exp(-4.5*0) + 0.1*F %// Where F has some value determined by previous
%// the function above and the previous value of t
%// -4.5*0 is because t = 0
M is now no longer a function, but a single scalar value. The next iteration t = 0.1. When you do: M=M(t)+h.*F; now, it interprets both the first and second M as a variable, not a function. t is therefore used as an index, instead of being an input parameter to the function M (since you have overwritten it).
When you are writing M(t), you are trying to access the 0.1'th element of the 1x1 matrix (scalar) M, which obviously isn't possible.
Additional notes:
The outer while loop has no purpose as it stands now, since i isn't reset after the inner loop. When you're finished with the first iteration of the outer loop, i is already >n, so it will never enter the inner loop again.
You shouldn't mix variable and function names (as you do with M. Use different names, always. Unless you have a very good reason not to.
data_out=[t,M]; is a growing vector inside a loop. This is considered very bad practice, ans is very slow. It's better to pre-allocate memory for the vector, for instance using data_out = zeros(k,1), and insert new values using indexes, data_out(ii) = M.
It's recommended not to use i and j as variable names in MATLAB as these also represent the imaginary unit sqrt(-1). This might cause some strange bugs if you're not paying attention to it.
You can almost certainly do what you're trying to do without loops. However, the function you have written is not functioning, and it's not explained all too well what you're trying to do, so it's hard to give advice as to how you can get what you want (but I'll give it a try). I'm skipping the dlmwrite-part, because I don't really understand what you want to output.
M = 5000;
t0 = 0;
tmax = 20;
h = 0.1; %// I prefer leading zeros in decimal numbers
t = t0: h: tmax;
data_out = M .* exp(-4.5 * t);
The problem is caused by M(t) in your code, because t is not an integer or logical (t=1,1.1,1.2,...)
You need to change your code to pass an integer as a subscript. Either multiply t by 10, or don't use the matrix M if you don't need it.

Error: Matrix dimensions must agree for plot

having a problem with my "new love", matlab: I wrote a function to calculate an integral using the trapz-method: `
function [L]=bogenlaenge_innen(schwingungen)
R = 1500; %Ablegeradius
OA = 1; %Amplitude
S = schwingungen; %Schwingungszahl
B = 3.175; %Tapebreite
phi = 0:2.*pi./10000:2.*pi;
BL = sqrt((R-B).^2+2.*(R-B).*OA.*sin(S.*phi)+OA.^2.*(sin(S.*phi)).^2+OA.^2.*S.^2.*(cos(S.*phi)).^2);
L = trapz(phi,BL)`
this works fine when i start it with one specific number out of the command-window. Now I want to plot the values of "L" for several S.
I did the following in a new *.m-file:
W = (0:1:1500);
T = bogenlaenge_innen(W);
plot(W,T)
And there it is:
Error using .*
Matrix dimensions must agree.
What is wrong? Is it just a dot somewhere? I am using matlab for the second day now, so please be patient.... ;) Thank you so much in advance!
PS: just ignore the german part of the code, it does not really matter :)
In your code, the arrays S and phi in the expression sin(S.*phi) should have same size or one of them should be a constant in order the code works
The error is most likely because you have made it so that the number of elements in schwingungen, i.e. W in your code, must be equal to the number of elements in phi. Since size(W) gives you a different result from size(0:2.*pi./10000:2.*pi), you get the error.
The way .* works is that is multiplies each corresponding elements of two matrices provided that they either have the same dimensions or that one of them is a scalar. So your code will work when schwingungen is a scalar, but not when it's a vector as chances are it has a different number of elements from the way you hard coded phi.
The simplest course of action (not necessarily the most Matlabesque though) for you is to loop through the different values of S:
W = (0:1:1500);
T = zeros(size(W); %Preallocate for speed)
for ii = 1:length(W)
T(ii) = bogenlaenge_innen(W(ii));
end
plot(W,T)
In your function you define phi as a vector of 10001 elements.
In this same function you do S.*phi, so if S is not the same length as phi, you will get the "dimensions must agree" error.
In your call to the function you are doing it with a vector of length 1501, so there is your error.
Regards