Solving and plotting spring constant vs Force
7 views (last 30 days)
Show older comments
I'm working on a simple spring question which requires me to plot the spring constant vs the force. I am given a number of constants and need to plot the Force for a given range for the k constant.
I am trying to obtain a value for "F" over a k range of 100000 to 800000. I can't seem to be able to obtain and store values of F in order to plot them against the k values.
here is my code:
%Declare constants and range along axis
c = 50*10^3;
x = 2.5*10^-2;
k = [100:1:800]*10^3;
for i = 1:length(k)
y = (F/k)+sqrt(F/c);
F(i) = solve(y == x, F);
end
%Plots the graph
plot(k, F)
xlabel('k');
ylabel('Force (N/m)');
title('Force vs. k');
What am I doing wrong?!
Thanks
0 Comments
Answers (1)
Roger Stafford
on 8 Feb 2014
Edited: Roger Stafford
on 8 Feb 2014
There is no need to use the symbolic function 'solve' at each of 701 steps. If properly used, 'solve' could give you a general formula, which you could then use to numerically compute the F that corresponds to each k.
Actually you don't even need 'solve' at all for this simple equation. It can easily be converted to a quadratic equation which you can use to solve for F as a function of k. My solution for that is:
F = x^2*k*c./(x*c+k/2+sqrt(x*k*c+k.^2/4));
(Note: The usual form of the quadratic equation solution would require a subtraction of two large and nearly equal quantities which reduces computation accuracy. Accordingly I have "rationalized" it to place the square root part in the denominator where these quantities are summed rather than subtracted, and that improves its accuracy.)
If obtaining a plot is the only requirement in your problem, you could just as well find k as a function of F which is even easier to do. It should be fairly easy to determine the interval of F values which will place k between the limits you have set for it. You could still do the plot as "plot(k,F)" even if it is now F that will be stepped off in equal increments.
0 Comments
See Also
Categories
Find more on Assembly in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!