Why I can't plot the following code? The error is:
Index exceeds the number of array elements (1).
What I'm trying to achive is that a user enters a scalar value f (such as 1500) anf Ht and Hr. So, I want to find value of ahr and L accondingliy and then plot L vs f. SO on f axis I shoud have 100,150,200,250,300,....,1500 and corrsponding values on L axis.
for i = 100:50:f
cm = 0;
ahr(i) = (1.1*log10(f(i))-0.7)*Hr-1.56*log10(f(i))-0.8;
L(i) = 46.3+33.9*log10(f(i))-13.82*log10(Ht)-ahr+(44.9-6.55*log10(Hr))*log10(d)+cm;
plot(f,L)
end
Using Vectorized Approach to Evaluate Plots
Assuming d, Ht and Hr are scalars the following code should work. This script uses a vectorized approach where f is a vector that is used to evaluate the equations ahr and L. The equations ahr and L will now be evaluated for every element in f resulting in the ahr and L being the same length as f. This method eliminates the need for a for-loop and works with MATLAB's strengths/capabilities.
%Asking user for input values%
fmax = input("Please type in the maximum f: ");
Ht = input("Please type in the value of Ht: ");
Hr = input("Please type in the value of Hr: ");
cm = 0;
d = 1;
%Creating the vector of points to plot on%
f = (100:50:fmax);
ahr = (1.1*log10(f)-0.7)*Hr-1.56*log10(f) - 0.8;
L = 46.3 + 33.9*log10(f)-13.82*log10(Ht)-ahr+(44.9-6.55*log10(Hr))*log10(d);
clf;
plot(f,L,'Marker','.');
title("Plot L vs f");
xlabel("f"); ylabel("L");
xticks(f);
grid;
Ran using MATLAB 2019b
Related
I have a loop where I symbolically solve for intervals. Each iteration of the loop gives exactly one interval, for example:
v=[x+1,-x+4];
v = v > 0;
sol=solve(v,x,'ReturnConditions',true);
cond = sol.conditions;
ezplot(cond)
Would I be able to plot the 1x1 symbolic array " cond = -1 < x & x < 4 " at a y value of my choosing? When I use ezplot it always plots at y=1.
Maybe by multiplying the cond by the y value you wish to use. In that case the title must be changed to match the original condition, cond by casting it as a string. In the example below I plotted the condition/region at a height of y = 2. To implement this in the for-loop I'd probably make a y a vector a retrieve one value/cell upon each iteration.
syms x
v = [x+1,-x+4];
v = v > 0;
sol = solve(v,x,'ReturnConditions',true);
cond = sol.conditions;
y = 2;
ezplot(y*cond);
ylim([0 y*1.2]);
title(string(cond));
First off; I'm not very well taught in programming, but i tend to learn exactly what I need to learn in order to do what I want when programming, I have moderate experience with python, html/css C and matlab. I've now enrolled in a physics-simulation course where I use matlab to compute the trajectory of 500 particles under the influence of 5 force-fields of different magnitude.
So now to my thing; I need to write the following for all i=1...500 particles
f_i = m*g - sum{(f_k/r_k^2)*exp((||vec(x)_i - vec(p)_k||^2)/2*r_k^2)(vec(x)_i - vec(p)_k)}
I hope its not too cluttered
And here is my code so far;
clear all
close all
echo off
%Simulation Parameters-------------------------------------------
h = 0.01; %Time-step h (s)
t_0 = 0; %initial time (s)
t_f = 3; %final time (s)
m = 1; %Particle mass (kg)
L = 5; %Charateristic length (m)
NT = t_f/h; %Number of time steps
g = [0,-9.81];
f = [32 40 28 16 20]; %the force f_k (N)
r = [0.3*L 0.2*L 0.4*L 0.5*L 0.3*L]; %the radii r_k
p = [-0.2*L 0.8*L; -0.3*L -0.8*L; -0.6*L 0.1*L;
0.4*L 0.7*L; 0.8*L -0.3*L];
%Forcefield origin position
%stepper = 'forward_euler'; % use forward Euler time-integration
fprintf('Simulation Parameters set');
%initialization---------------------------------------------------
for i = 1:500 %Gives inital value to each of the 500 particles
particle{i,1}.x = [-L -L];
particle{i,1}.v = [5,10];
particle{i,1}.m = m;
for k = 1:5
C = particle{i}.x - p(k,:);
F = rdivide(f(1,k),r(1,k)^2).*C
%clear C; %Creates elements for array F
end
particle{i}.fi = m*g - sum(F); %Compute attractive force on particle
%clear F; %Clear F for next use
end
What this code seems to do is that it goes into the first loop with index i, then goes through the 'k'-loop and exits it with a value for F then uses that last value for F(k) to compute f_i.
What I want it to do is to put all the values of F(k) from 1-5 and put into a matrix which columns I can sum for f_i. I'd prefer to sum the columns as the first column should represent all F-components in the x-axis and the second column all F-components in the y-axis.
Note that the expression for F in the k-loop is not done.
I fixed it by defining the index for F(k,:)
I'm trying to do something like the following code:
k = linspace(a,b);
x = c:0.01:d;
% k and x are of different sizes
f = #(s) arrayfun(#(t) normcdf(s, b0+b1*t, sigma), x);
y = arrayfun(f, k);
I get the following error
Error using arrayfun Non-scalar in Uniform output, at index 1,
output 1. Set 'UniformOutput' to false.
I'm trying to avoid using a for loop for each element in k.
Also, for each result matching an element in k, I need to do another small calculation
Example with a loop:
for i=1:m % m is the number of elements in k
f = #(t) normcdf(k(i), b0+b1*x, sigma);
y = arrayfun(f, x);
res(i) = trapz(x,y);
end
any idea how can I get the same result as the for loop with the first method?
and why am I getting the error?
I would suggest the following:
k = linspace(a,b);
x = c:0.01:d;
[X,K] = meshgrid(x,k)
y = arrayfun(#(p,t) normcdf(p, b0+b1*t, sigma), K(:), X(:))
res = cumtrapz(x,y)
Untested though as you gave no example data and desired results. Maybe you need to swap the order of x and k as well as X and K to get the desired result. (or use ndgrid instead of meshgrid, has the same effect)
Hello I am relatively new to MATLAB and have received and assignment in which we could use any programming language. I would like to continue MATLAB and have decided to use it for this assignment. The questions has to do with the following formula:
x(t) = A[1+a1*E(t)]*sin{w[1+a2*E(t)]*t+y}(+/-)a3*E(t)
The first question we have is to develop an appropriate discretization of x(t) with a time step h. I think i understand how to do this using step but because there is a +/- in the end I am running into errors. Here is what I have (I have simplified the equation by assigning arbitrary values to each variable):
A = 1;
E = 1;
a1 = 1;
a2 = 2;
a3 = 3;
w = 1;
y = 0;
% ts = .1;
% t = 0:ts:10;
t = 1:1:10;
x1(t) = A*(1+a1*E)*sin(w*(1+a2*E)*t+y);
x2(t) = a3*E;
y(t) = [x1(t)+x2(t), x1(t)-x2(t)]
plot(y)
The problem is I keep getting the following error because of the +/-:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in Try1 (line 21)
y(t) = [x1(t)+x2(t), x1(t)-x2(t)]
Any help?? Thanks!
You can remove the (t) from the left-hand side of all three assignments.
y = [x1+x2, x1-x2]
MATLAB knows what to do with vectors and matrices.
Or, if you want to write it out the long way, tell MATLAB there will be two columns:
y(t, 1:2) = [x1(t)'+x2(t)', x1(t)'-x2(t)']
or two rows:
y(1:2, t) = [x1(t)+x2(t); x1(t)-x2(t)]
But this won't work when you have fractional values of t. The value in parentheses is required to be the index, not a dependent variable. If you want the whole vector, just leave it out.
I have a 30-vector, x where each element of x follows a standardised normal distribution.
So in Matlab,
I have:
for i=1:30;
x(i)=randn;
end;
Now I want to create 30*30=900 elements from vector, x to make a 900-vector, C defined as follows:
I am unable to do the loop for two variables (k and l) properly. I have:
for k=1:30,l=1:30;
C(k,l)=(1/30)*symsum((x(i))*(x(i-abs(k-l))),1,30+abs(k-l));
end
It says '??? Undefined function or method 'symsum' for input arguments of type
'double'.'
I hope to gain from this a 900-vector, C which I will then rewrite as a matrix. The reason I have using two indices k and l instead of one is because I eventually want these indices to denote the (k,l)-entry of such a matrix so it is important that that my 900-vector will be in the form of C = [ row 1 row 2 row 3 ... row 30 ] so I can use the reshape tool i.e.
C'=reshape(C,30,30)
Could anyone help me with the code for the summation and getting such a 900 vector.
Let's try to make this a bit efficient.
n = 30;
x = randn(n,1);
%# preassign C for speed
C = zeros(n);
%# fill only one half of C, since it's symmetric
for k = 2:n
for l = 1:k-1
%# shift the x-vector by |k-l| and sum it up
delta = k-l; %# k is always larger than l
C(k,l) = sum( x(1:end-delta).*x(1+delta:end) );
end
end
%# fill in the other half of C
C = C + C';
%# add the diagonal (where delta is 0, and thus each
%# element of x is multiplied with itself
C(1:n+1:end) = sum(x.^2);
It seems to me that you want a matrix C of 30x30 elements.
Given the formula that you provided I would do
x = randn(1,30)
C = zeros(30,30)
for k=1:30
for l=1:30
v = abs(k-l);
for i =1:30-v
C(k,l) = C(k,l) + x(i)*x(i+v);
end
end
end
if you actually need the vector you can obtain it from the matrix.