Using a array in a equation that has a variable being isolated.

I've been assigned to use MatLab to make a graph of the height a projectile is released from a ramp vs the distance it travels. As far as i know i have everything required but isolating t as a function of several other functions including the array h. No mater what I do I still get the error message "Matrix dimension must agree". Can anyone help?
clc
%Setting heights and velocity equations
%h=height,g=gravity,v=velocity
%vx=horizontal velocity,vy=vertical velocity
g=9.8;
h=[0.1:0.1:1];
v=sqrt(2*g*h);
vy=v*sind(45);
vx=v*cosd(45);
%defining t
%yp=vertical displacement against time
%h=height
y = ((h+(vy.*t))-(.5.*g.*t.^2) == 0);
t = solve(y,t);
%finding range
%r=range
r=vx.*t;
%Making a graph based of height against range
plot(h,r)
xlabel('height')
ylabel('range')

2 件のコメント

John D'Errico
John D'Errico 2017 年 9 月 2 日
Are there two solutions to that quadratic equation? Solve will give them both to you.
John McClarin
John McClarin 2017 年 9 月 2 日
Image Analyst showed my the root() command, so im going to use that. Getting the array to work in the equation is the main problem.

サインインしてコメントする。

回答 (2 件)

Star Strider
Star Strider 2017 年 9 月 1 日

0 投票

Try this:
g=9.8;
h=[0.1:0.1:1];
v=sqrt(2*g*h);
vy=v*sind(45);
vx=v*cosd(45);
%defining t
%yp=vertical displacement against time
%h=height
for k1 = 1:numel(h)
y = @(t) (h(k1)+(vy.*t))-(.5.*g.*t.^2);
t(k1) = fsolve(y, 1);
end
%finding range
%r=range
r=vx.*t;
%Making a graph based of height against range
plot(h,r)
xlabel('height')
ylabel('range')

4 件のコメント

John McClarin
John McClarin 2017 年 9 月 1 日
It doesn't work. the fsolve comes up with no solutions found, and the 'matrix dimensions must agree' warning is still there.
Warning: Trust-region-dogleg algorithm of FSOLVE
cannot handle non-square systems; using
Levenberg-Marquardt algorithm instead.
> In fsolve (line 298)
No solution found.
fsolve stopped because the last step was ineffective. However, the vector of function
values is not near zero, as measured by the default value of the function tolerance.
<stopping criteria details>
Matrix dimensions must agree.
Star Strider
Star Strider 2017 年 9 月 2 日
It worked when I ran it or I’d not have posted the code.
John McClarin
John McClarin 2017 年 9 月 2 日
Yeah I loaded it up this morning and it worked fine, I don't know what was different yesterday. Maybe the workspace was storing values from my other tries? Im knew and don't know if that could affect it but I had maybe a dozen variables in there from previous attempts.
Star Strider
Star Strider 2017 年 9 月 2 日
Did my Answer solve your problem?

サインインしてコメントする。

Image Analyst
Image Analyst 2017 年 9 月 1 日

0 投票

See my projectile demo. It computes and plots just about everything you could want. Here are the plots. Other info is given in message boxes.
Adapt as needed.

3 件のコメント

John McClarin
John McClarin 2017 年 9 月 2 日
That's a great program, but the problem is still that I have to create a script that can handle a array as a variable (or something similar). Using the root(a,b,c) simplifys things a bit but in this case c is a array, which gets the 'matrix dimensions must agree'.
Image Analyst
Image Analyst 2017 年 9 月 2 日
I don't understand. What is the "a array"? And why is it not a variable? And why is c, which is y0 = the initial height, an array? "a" and "c" in my code are scalars. Post your new code.
John McClarin
John McClarin 2017 年 9 月 2 日
The other guys loop fixed it (mostly). My bad on the grammar, it should have been an 'array', that was the initial height i released the object from on a ramp and as im writing this i realize the h in my time-to-fall equation should be a constant of the height of the end ramp, where the vy should be the only function of h.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2017 年 9 月 1 日

コメント済み:

2017 年 9 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by