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

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 comentarios

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.
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".

Iniciar sesión para comentar.

 Respuesta aceptada

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 comentarios

THANK YOU SO SO SO MUCH!!!! You've been an immense help :D .. I do have a question though if you have time at all! I really appreciate this. So when I run the script, sinx and cosx both give me a 1x13 array and since x is a 1x20 array, that means sin(x) will also be a 1x20 array. So this is what I'm getting in my command window: Range of integration: [0:4*pi] Number of terms to evaluate: 20 Range of integration: [0:4*pi] Number of terms to evaluate: 20 Error using plot Vectors must be the same lengths.
Error in assmt5 (line 11) h1 = plot(x,sinx);axis tight;grid on
EDU>>
I'm presuming there's a way to 'linspace' everything but I'm not quite sure how to do that because everytime I try it's changing the outcome :(.
Also we've only just had one class on user defined functions so it took me a while to figure it out, still not sure if it's accurate so I'll attach my scripts. My two functions are saved in my library subfolder.
THANK YOU SOOOOOOOOOOOOO MUCH!!!!
So for sinx I was thinking of using sinx = linspace(-1,0.9894,20) .. -1 is the smallest value and 0.9894 was the largest value given in 1/13 array .. so this turns it into a 1x20 array and for cosx I'm using cosx = linspace(-0.9900,1,20) and the graph they're giving me is attached.
Clara
Clara el 20 de Mzo. de 2015
Editada: Clara el 20 de Mzo. de 2015
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 :(
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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Preguntada:

el 19 de Mzo. de 2015

Editada:

el 20 de Mzo. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by