Need help creating a graph
4 views (last 30 days)
Show older comments
Hello, I'm new to Matlab and im trying to create graphs on here. It relates to Static force delfection, where its the deflection of a spring under static force. The equation is:
X=Xsubscript(SF)/(1-lambda^2)
This graph has X(sub SF) on the y axis, and the matlab graph shows the different values of lambda. (If lambda less than one and if lambda is greater than one)
I've never used Matlab or anything coding related so can anyone help me write or point me in the direction of how to write a matlab script from the given equation? Thank you very much for your help!
0 Comments
Answers (2)
James Browne
on 12 Jun 2019
Edited: James Browne
on 12 Jun 2019
Greetings,
I think I can help you out. I wrote the following code which I thik will produce the plot that you are after:
%Assign values of lamda from 0.01 to 2, in increments of 0.001
lambda = 0.01:0.001:2;
%assign XsubscriptSF value
XsubscriptSF = 2;
%calculate number of lamda values to be used
n = length(lambda);
%preallocate memory to store X values
X = zeros(1,n);
%Calculate X values
for i = 1:n
X(i) = XsubscriptSF/(1-lambda(i)^2);
end
plot(lambda,X)
title('Lambda VS Static Deflection for X_{SF}')
xlabel('Lambda (units)')
ylabel('X_{SF} (units)')
A few things to note, the green text in the above code are comments and do nothing but convey information to the user. Additionally, MATLAB has some very nice help pages for just about everything you need to know to do the basics, although sometimes it is hard to find the right page because you may not know the terminology for the concept that you are interested in, I usually find what I am looking for by typing what I want to do into google followed by the word MATLAB.
For example, if you were interested in learning more about the first line of code where the lambda values are assigned and how that syntax works, the best help page is actually the page for the colon ( : ) operator, found here:
Now that seems a bit out there but I found that page by typing "create a vector of numbers matlab" into google, it was the third result down so it was not too bad!
It is even easier if you already know the name of the function that you want help with, for example, if you wanted to learn more about how the plot() function works, you can just google "plot matlab" and it will take you strait to the help page, which has tons of great examples of how to do the basics. Also, you can type "help plot" (without the quotation marks) into the command terminal in MATLAB and it will bring up most of the same information that is in the help page, right in the command terminal window!
One more thing too, when you are navigating the help pages, at the bottom of every help page, there is a "See Also" section which lists links to related help pages, I often find what I am looking for with the See Also links, if I do not find it on the first help page that I look for. The image below shows the See Also section for the colon operator help page. As you can see, it has a link to the "for" operator so you can check out how the for loop works that I used to do the calculations in the example code.
One last thing, the calculations in the for loop in the example code above can also be accomplished in one line of code as follows:
X = XsubscriptSF./(1-(lambda.^2));
I prefer using for loops though, they are more versitile and easier to troubleshoot than elementwise calculation equations, which the line above is, particularly when there are a lot of vectors and or matrices involved!
Good luck, hope this helps~
Amra Feta
on 10 Nov 2020
Edited: Amra Feta
on 10 Nov 2020
I have also a question I have created the code for my matrix in queuing network(more focused on Open Queuing Network). However I am new to Simulink and I need some help in plotting and creating a graph(not sure if I need to use Simulink).What I want to do is get time into the picture of arrival rates, to find the maximum arrival rate and how it behaves.
Here is my code:
N=6
M_sparse=sprand(N,N+1,0.5)
M=full(M_sparse)
M(1:1+size(M,1):end)=0
R=M(1:end,1:end-1)
determinant=det(eye(N)-R)
Q=inv(eye(N)-R)
gamma=randi([0 1],1,N)
lambda=gamma*Q
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!