Parse error Simple Function Problem

7 次查看(过去 30 天)
Hi I am brand new to matlab and am writing a simple program for an assignment. We are given a piecewise function with three parts of the height of a rocket. and are supposed to write a function to calculate the height. I did this but am getting a parse error for the t's inside the if and if elses. Any guidance is much appreciated!
Function[y]=height(t)
if(t<0)
y=0;
elseif(t<15)
y=38.1454t+0.13743t^3;
elseif((t<33)&&(t>=15)
y=1036+103.909(t-15)+6.18425(t-15)^2-0.428(t-15)^3;
elseif(t>33)
y=2900-62.468(t-33)-16.9274(t-33)^2+0.41796(t-33)^3
end

采纳的回答

Star Strider
Star Strider 2018-1-15
You need to specify the multiplication operators (*). I added element-wise exponentiation. You also need to save your function to its own file as height.m on your MATLAB user path.
This is not as efficient as it could be, however it will get you started:
function [y]=height(t)
if (t<0)
y=0;
elseif (t<15)
y=38.1454*t+0.13743*t.^3;
elseif (t<33) & (t>=15)
y=1036+103.909*(t-15)+6.18425*(t-15).^2-0.428*(t-15).^3;
elseif (t>33)
y=2900-62.468*(t-33)-16.9274*(t-33).^2+0.41796*(t-33).^3;
end
end
t = linspace(0, 50, 50);
for k1 = 1:numel(t)
h(k1) = height(t(k1));
end
figure
plot(t, h)
grid
Experiment to get the result you want.
  1 个评论
Walter Roberson
Walter Roberson 2018-1-15
One thing to pay attention to here is that Star Strider took care to pass in only a single numeric value instead of passing in a vector of values. Your code is not designed to handle vectors of values. You should consider using logical indexing.

请先登录,再进行评论。

更多回答(1 个)

iorngbough iorpinen
Hi please someone should help out,iam new in MATLAB iam writing a program on two first order differential equation using shooting method and iam getting 'parse'error at the first bracket after the "equal to sign"
Function [dp]=(r,p) E=50;q1=4;q2=-2;B=1672;rho=0.32×10^-9;l=1; do=[p(2);(-2*E-2*q1*q2*r^-1-2*B*exp(-r/rho)+l*(l+1)*r^-2*p(1)];
  3 个评论
Star Strider
Star Strider 2019-10-6
@iorngbough iorpinen —
Please post your Answer as a new Question. It does not relate to this thread.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by