Empty plot is showing because of my er

조회 수: 5 (최근 30일)
Panda05
Panda05 2023년 11월 30일
댓글: Panda05 2023년 11월 30일
Hello guys , is there a way i can store my er as a vector that i can later on use to plot with N ? thank you for your suggestions. The code is shown below. I just want to be able to use er in the for loop to plot with N , how can i retrieve the er values into a vector
N=[10,20,40,80];
for i = 1:length(N)
x = (0:N(i))./N(i);
xsamples = 0:0.005:1;
y = f(xsamples);
fxn = Lagpoly(xsamples,x,y,N);
er= max(abs(fxn-y));
end
plot(N, er, '*-', 'DisplayName', 'Data Points');
%plot(N,N);
%set(gca, 'XScale', 'log')
%set(gca, 'YScale', 'log')
function y = f(xsamples)
y = 1 ./ (1+ (xsamples.*xsamples));
end
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2023년 11월 30일
편집: Dyuman Joshi 2023년 11월 30일
Even if the er is a scalar, the code should plot something, see below.
If you get an empty plot, your data could have NaN values.
Please attach the defition of the parameter Lagpoly, so that we can run the code and reproduce the error.
plot(1:10, 10, '*-')

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

채택된 답변

Image Analyst
Image Analyst 2023년 11월 30일
Index er. This works:
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
N = [10, 20, 40, 80];
for k = 1:length(N)
x = (0:N(k))./N(k);
xsamples = 0:0.005:1;
y = f(xsamples);
% fxn = Lagpoly(xsamples,x,y,N);
fxn = rand;
er(k) = max(abs(fxn-y));
end
% Plot on a log-log scale. Or use plot() for a linear scale.
loglog(N, er, '*-', 'DisplayName', 'Data Points',...
'LineWidth', 2, 'MarkerSize', 30);
grid on;
xlabel('N', 'FontSize', fontSize);
ylabel('er', 'FontSize', fontSize);
title('er vs. N', 'FontSize', fontSize);
%=======================================================================
function y = f(xsamples)
y = 1 ./ (1+ (xsamples.*xsamples));
end
Since you didn't provide Lagpoly I had to just use a random number to test it. Replace the rand line with your Lagpoly line of code.
To learn other fundamental concepts, invest 2 hours of your time here:

추가 답변 (1개)

Torsten
Torsten 2023년 11월 30일
편집: Torsten 2023년 11월 30일
What x-y data are given to build the Lagrange Polynomial and in which x-data do you want to interpolate ? I assume that x-y are given arrays to build the Lagrange Interpolation upon and that you want to interpolate in xsamples. But your code takes xsamples and y to build the Lagrange polynomial and x for evaluation. I don't think this is what you want.
Further, you have to save "er" for each value of i.
Use
er(i)= max(abs(fxn-y));
instead of
er= max(abs(fxn-y));

카테고리

Help CenterFile Exchange에서 Smoothing and Denoising에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by