HELP!! Seriously struggling with my final MATLAB assignment! I've been doing it for over 6 hours now and I'm ridiculously stuck :( if anyone could help at all I would REALLY appreciate it :( THANK YOUUU

조회 수: 11 (최근 30일)
Attached is my code and assignment. I haven't included cos(x) yet because I can't figure out sin(x). My code is pretty pathetic!! I understand everything we've done up to loops but this is killing me .. If anyone has any input at all I'd really appreciate it!! Thank you :)
  댓글 수: 2
Star Strider
Star Strider 2015년 3월 19일
Take a deep breath and relax!
I haven’t run your code, so tell us what it’s not doing that it should, or what it’s doing that it shouldn’t.
Remember, you have to do this for (0 <= x <= 4*pi) so be sure to define your ‘x’ vector, and supply the relevant elements of it to your ‘xbsin’ calculation at each step. The assignment wants at least 20 values of ‘x’ for your code as it increases the number of terms, so define your ‘x’ vector first. Iterate through that at each step as you add terms to your series. Remember, ‘epsilon’ has to take the difference of the entire series at each step as you add terms.
I would run this on paper first, calculating it with a hand calculator for a particular value of ‘x’, perhaps pi/4 and 5*pi/4. That will give you a general idea of what you’re doing.
Stephen23
Stephen23 2015년 3월 20일
편집: Stephen23 2015년 3월 20일
Judging by the Mfile that you attached you already know enough MATLAB code to be able to solve this yourself. The challenge for you now is not writing the code but rather understanding how to solve the problem. Yes, there is a big difference between these! Break the problem down and do not try to write a whole page of code without actually knowing how it really works... this is doomed to be a struggle.
You could even, as Star Strider suggests, solve this first on some paper: go through some iterations, see what values you need to keep track of and when these work successfully convert them into code form. While you are working through by hand, consider which values are useful to keep, give them sensible names and write them out in a table. You can compare these with your code later.
You goal now is not code writing but to figure out an algorithm, and this is much more abstract thinking... but being systematic and using all of your available resources will get you there :)
I highly recommend reading the book "How to Solve It" by George Polya, which gives very good advice on how to approach problem-solving tasks like this one: note that the book's first principle is also "Understand the problem".

댓글을 달려면 로그인하십시오.

채택된 답변

Konstantinos Sofos
Konstantinos Sofos 2015년 3월 20일
편집: Konstantinos Sofos 2015년 3월 20일
Hi,
I agree fully with the above comments. Take a deep breath and relax! and that by the Mfile that you attached you already know enough MATLAB code. I will give you my solution but be sure that you have to understand the problem and how someone can formulate it into a program. This is the final scope of this assignment.
function [sinx,error] = sinx_approx(x,n)
% Approximates the value of sin(x), the approximation is more accurate as
% the number of terms selected is increased.
%
% INPUTS
% x range of integration
% n Up to how many terms would you like to evaluate?
% OUTPUTS
% sinx approximation of sin(x)
% error tollerance error
sinx=0;
error = 1;
mytol = 0.001;
while error > mytol
temp=sinx;
for i=1:n
sinx=(-1)^(i+1) * x.^(2*i-1)/ factorial(2*i-1)+ sinx;
error=abs(sinx-temp);
end
end
And for the cosine
function [cosx,error] = cosx_approx(x,n)
% Approximates the value of cos(x), the approximation is more accurate as
% the number of terms selected is increased.
%
% INPUTS
% x range of integration
% n Up to how many terms would you like to evaluate?
% OUTPUTS
% cosx approximation of sin(x)
% error tollerance error
cosx=1;
error = 1;
mytol = 0.001;
while error > mytol
temp=cosx;
for i=1:n
cosx=(-1)^(i) * x.^(2*i)/ factorial(2*i)+ cosx;
error=abs(cosx-temp);
end
end
Then is the main function which calls the above and plot the result
%--Assignment5.m
%
% Description: This program estimates the values for sin(x) and cos(x) for
% x, in the range of [0,4pi]. This program also displays the approximate
% and exact values of sin(x) and cos(x) in a graph.
clear;
clc;
% Generating 20 values of x between 0 and 4pi.
x = linspace(0,4*pi,20);
% Approximate sinx and cosx
sinx = sinx_approx(x,20);
cosx = cosx_approx(x,20);
% plot
subplot(1,2,1)
h1 = plot(x,sinx);axis tight;grid on
set(h1,'Marker','.','Color','red','MarkerSize',15)
hold on
plot(x,sin(x),'b--');
title('Sine')
legend({'Approximated','Exact'})
subplot(1,2,2)
h2=plot(x,cosx);axis tight;grid on
set(h2,'Marker','.','Color','red','MarkerSize',15)
hold on
plot(x,cos(x),'b--');
title('Cosine')
legend({'Approximated','Exact'})
%
  댓글 수: 4
Clara
Clara 2015년 3월 20일
편집: Clara 2015년 3월 20일
This is inaccurate as it puts the numbers in order from lowest to highest values, so I'm wondering how to keep them in the same order but ensure the vectors are the same length so they can both be plotted on the same axis. I thought sinx and cosx should give me a 1x20 array also. I've been trying to figure this out for hours now and can't at all :(
Konstantinos Sofos
Konstantinos Sofos 2015년 3월 20일
편집: Konstantinos Sofos 2015년 3월 20일
Hi,
I cannot understand where are you struggling. Why have you changed the sinx_approx and cosx_approx functions and you ask for x,n again as inputs from the command line from the time that you set them as inputs from the function??? If you run the attached you will have something like that

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by