Loop until a condition is met
显示 更早的评论
How can i do a function that give values for P0, P1, P2 and P3 until the condition Rt<Rmax is met
h=0.001;
Rmax=zeros(1,length(v));
t=zeros(1,length(v));
x=zeros(1,length(v));
for i=1:length(v)
t(i)=(v(i)-v(0))/a;
x(i)=v(0)*t(i)+(1/2)*a;
Rmax(i)=(v(i)^2)/amax;
end
B(x)=(1-x)^3*P0+3*x*(1-x)^2*P1+3*x^2*(1-x)*P2+t^3*P3;
Rt=(1+(diff(B)/h)^2)^(3/2)/(diff(B,2)/h);
end
8 个评论
Dyuman Joshi
2023-3-20
编辑:Dyuman Joshi
2023-3-20
Few things -
Indexing in MATLAB starts with 1, not 0, thus these lines will lead to an error
t(i)=(v(i)-v(0))/a;
x(i)=v(0)*t(i)+(1/2)*a;
If you want to refer it the first element, use v(1).
Secondly, you can vectorize some parts of your code
%Assuming a and amax are a scalar constant
h=0.001;
%define a value as a variable if you have to use it many times
len=length(v);
%Rmax=zeros(1,len);
%t=zeros(1,len);
%x=zeros(1,len);
%Note that, as the code uses another variable to directly define
%t, x and Rmax, I have chosen not to preallocate them
%Assuming you want to refer to the first element
t=(v-v(1))/a;
x=v(1)*t+(1/2)*a;
Rmax=(v.^2)/amax;
% ^
%Since v is an array, it requires element-wise operation
However, it's not clear what you want to achieve with B and subsequently Rt.
B(x)=(1-x)^3*P0+3*x*(1-x)^2*P1+3*x^2*(1-x)*P2+t^3*P3;
Rt=(1+(diff(B)/h)^2)^(3/2)/(diff(B,2)/h);
Is B supposed to be a polynomial in x? If not then specify what it is supposed to be.
Are P0, P1, P2 and P3 supposed to be unknown whose value you have to find? If they are unknowns, how is one supposed to find the value of Rt to compare with Rmax?
And to which value of Rmax do you want to compare? As Rmax is an array.
Jon Bilbao
2023-3-21
Dyuman Joshi
2023-3-21
Alright.
What is t supposed to be here? The variable defined above in terms of v?
If yes, then you will have a vector as a coefficient. If no, then please specify.
syms X P0 P1 P2 P3
B(X)=(1-X)^3*P0+3*X*(1-X)^2*P1+3*X^2*(1-X)*P2+t^3*P3;
Rt=(1+(diff(B)/h)^2)^(3/2)/(diff(B,2)/h)
It would be helpful if you can attach the data for v, as all calculations depend on it.
Jon Bilbao
2023-3-21
Dyuman Joshi
2023-3-21
Okay, it was a typo.
Please attach the data for v, as I mentioned earlier as well.
Jon Bilbao
2023-3-21
Dyuman Joshi
2023-3-21
Again, the for loop can be vectorized
l=0:0.1:Lt;
v=(v0.^2+2*a*l).^(1/2);
What are the inputs to the function VelTren? Please provide the values of v0, a, Lt and amax.
John, please note that when I say data, it means anything that is necessary to run the code, including definitions of variables and input values to functions.
This might be helpful to you, as that is the norm on this forum.
Jon Bilbao
2023-3-21
回答(1 个)
Sachin
2023-3-20
Hi @Jon Bilbao
I understand that you want to find values P0,P1,P2 and P3 until Rt<Rmax.
For this you can use while loop with condition Rt<Rmax;
while (Rt < Rmax)
% your code here
end
类别
在 帮助中心 和 File Exchange 中查找有关 Mathematics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!