# how to plot the equation

1 view (last 30 days)
shiv gaur on 21 Feb 2022
Commented: Image Analyst on 21 Feb 2022
x(0)=1;
y(0)=0
t=1:15;
for n=1:15
x(n+1)=(x(n-1)-y(n))/n+1;
y(n+1)=(x(n)+y(n)-t)/n+1
plot(t,x)
end
##### 2 CommentsShowHide 1 older comment
Image Analyst on 21 Feb 2022
Yeah, I figured that, but what I meant by my answer below is you're supposed to do
x(1) = 1;
y(1) = 0;

Image Analyst on 21 Feb 2022
Edited: Image Analyst on 21 Feb 2022
Indexing starts at 1, not 0. Also, check your formulas - they look weird.
For example in
y(n+1)=(x(n)+y(n)-t)/n+1
you need t(n), not just t which is the entire array.
y(n+1)=(x(n)+y(n)-t(n))/n+1
Image Analyst on 21 Feb 2022
OK, so it's all working now that you're starting your vectors at index 1 instead of 0, and changed t to t(n)? If so, can you accept this answer because I pointed out the problems? If not, post your current code so we can continue to work on it. This is what I got. Let me know if it's the same as what you have.
x = zeros(1, 16);
y = zeros(1, 16);
x(1)=1;
% y(1)=0
t=1:15;
for n = 2: length(x) - 1
fprintf('n = %d.\n', n);
x(n+1) = (x(n-1)-y(n)) / n + 1; % Note only n is in the denominator, not (n+1)
y(n+1) = (x(n)+y(n)-t(n)) / n + 1;
end
n = 2. n = 3. n = 4. n = 5. n = 6. n = 7. n = 8. n = 9. n = 10. n = 11. n = 12. n = 13. n = 14. n = 15.
plot(t, x(1:end-1), 'b.-', 'LineWidth', 2, 'MarkerSize', 30);
hold on;
plot(t, y(1:end-1), 'r.-', 'LineWidth', 2, 'MarkerSize', 30);
grid on;
xlabel('t', 'FontSize', 20);
ylabel('x or y', 'FontSize', 20);
title('x or y VS. t', 'FontSize', 20);
legend('x', 'y') R2021b

### Community Treasure Hunt

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

Start Hunting!
{"ft_lang_label":"__label__en","ft_lang_prob":0.7686543,"math_prob":0.99691266,"size":1612,"snap":"2022-27-2022-33","text_gpt3_token_len":592,"char_repetition_ratio":0.087064676,"word_repetition_ratio":0.014336918,"special_character_ratio":0.39826304,"punctuation_ratio":0.17090069,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9999337,"pos_list":[0,1,2],"im_url_duplicate_count":[null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-16T07:45:22Z\",\"WARC-Record-ID\":\"<urn:uuid:978b2069-cfe9-4b98-8798-28b97f14ea33>\",\"Content-Length\":\"156275\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8a4705f4-5745-4ae3-8af9-e4ad36c62f35>\",\"WARC-Concurrent-To\":\"<urn:uuid:1d6f0213-06be-40e5-aaf9-fc8bc5f6113e>\",\"WARC-IP-Address\":\"23.221.210.185\",\"WARC-Target-URI\":\"https://it.mathworks.com/matlabcentral/answers/1655210-how-to-plot-the-equation?s_tid=prof_contriblnk\",\"WARC-Payload-Digest\":\"sha1:6HQTG5GAKVOXT6MSF7JNPWPD5I4MNWMI\",\"WARC-Block-Digest\":\"sha1:QS3CK2U75INJXT3JBKGXPA7PX6PFWRYY\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882572221.38_warc_CC-MAIN-20220816060335-20220816090335-00019.warc.gz\"}"}